Codeigniter:如何获得调整大小的图像路径?

时间:2016-03-21 10:49:39

标签: php codeigniter

我正在使用codeigniter的默认库来调整我的图像大小以获得不同的拇指。我为此提到了Documentation codeigniter。

我的图片调整得很好。现在,我想在数据库中存储已调整大小的图像路径。为此,我检查了库文件和其他细节,但没有找到一种方法来获得调整大小的图像路径。

请检查我的代码

protected function createThumbs($params)
{                               
    if( !is_dir($params['targetPath']) ) {
        mkdir($params['targetPath'], 0777, TRUE);
    }

    $tConfig['image_library'] = 'gd2';
    $tConfig['source_image'] = $params['sourcePath'];                               
    $tConfig['new_image'] = $params['targetPath'];
    $tConfig['create_thumb'] = TRUE;
    $tConfig['maintain_ratio'] = TRUE;
    $tConfig['width'] = $params['width'];
    $tConfig['height'] = $params['height'];     

    $this->load->library('image_lib', $tConfig);                                
    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
     }
    // clear //
    $this->image_lib->clear();
}

有谁知道如何获得它?

3 个答案:

答案 0 :(得分:1)

尝试

protected function createThumbs($params)
{                               
    if( !is_dir($params['targetPath']) ) {
        mkdir($params['targetPath'], 0777, TRUE);
    }

    $tConfig['image_library'] = 'gd2';
    $tConfig['source_image'] = $params['sourcePath'];                               
    $tConfig['new_image'] = $params['targetPath'];
    $tConfig['create_thumb'] = TRUE;
    $tConfig['maintain_ratio'] = TRUE;
    $tConfig['width'] = $params['width'];
    $tConfig['height'] = $params['height'];     

    $this->load->library('image_lib', $tConfig);                                
    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
     }
    // clear
    $this->image_lib->clear();
    #return
    $file_info = pathinfo($tConfig['new_image']);
    return  $file_info['dirname'].'/'.$file_info['filename'].'_thumb.'.$file_info['extension'];
}

答案 1 :(得分:0)

您的拇指文件将在文件名末尾使用@EnableGlobalMethodSecurity进行调用。 例如,如果您的文件是:_thumb,则您调整大小的拇指将被称为image.jpg

答案 2 :(得分:0)

public function addUserImages()
    {

        $image                       =      preg_replace("/\s+/", "_", $_FILES['user_image']['name']);
        $config['upload_path']       =      "./Images/ProfileImages/";
        $config['allowed_types']     =      "gif|jpg|png|jpeg|JPG|JPEG|PNG|GIF"; 
        $config['file_name']         =      $image;
        $this->load->library('upload',$config);
        if($this->upload->do_upload("user_image")==false) 
        {
                $error = array('error' => $this->upload->display_errors());
                echo $error['error'];
                return $error->error;
        }
        else
        {
            $data               = $this->upload->data();
            $newImage       = $data['file_name'];
            $config['image_library']   = 'gd2';
            $config['source_image']    = './Images/ProfileImages/'.$newImage;
            $config['new_image']       = './Images/ProfileImages/small_'.$newImage;
            $config['create_thumb']    = FALSE;
            $config['maintain_ratio']  = TRUE;
            $config['quality']         = '100';
            $config['width']           = 250;
            $config['height']          = 250;
            $this->image_lib->clear();
            $this->image_lib->initialize($config);
            $this->load->library('image_lib', $config);
            if(!$this->image_lib->resize())
            {
              return  $this->image_lib->display_errors(); 

            }
        else {
            $newImage   =   "small_".$newImage;


            }
        }

    }