Gokul Shinde
Gokul Shinde

Reputation: 965

Codeigniter: How to get resized image path?

I am using default library of codeigniter to resize my image to get different thumbs. I have refer to Documentation of codeigniter for this.

My images gets resize fine. Now, I want to store resized image path in database. For that I checked library file and other details as well but not found a way to get resized image path.

Please check my code

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();
}

Does anyone knows how to get it?

Upvotes: 0

Views: 1068

Answers (3)

Manish
Manish

Reputation: 29

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;


            }
        }

    }

Upvotes: 0

Arkadiusz G.
Arkadiusz G.

Reputation: 1086

Try

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'];
}

Upvotes: 1

Daniel Dudas
Daniel Dudas

Reputation: 3002

Your thumb file will be called with _thumb at the end of file name. For example if your file is: image.jpg your resized thumb will be called image_thumb.jpg

Upvotes: 0

Related Questions