prakashchhetri
prakashchhetri

Reputation: 1816

Codeigniter image resize resizing only one image

What I am basically trying to do is resize a image into two different size images. However I am only being able to resize the first featured_$filename I am not being able to resize thumb_$filename .

Initially I tried creating a single function and passed the configuration as an array and it didnt worked but same thing, only single image was resized.

    $this->resizeImage($imagePath, $file['upload_data']['file_name']);


    $this->resizeThumb($imagePath, $file['upload_data']['filename']);

public function resizeImage($imagePath, $filename){
        $config['image_library'] = 'gd2';
        $config['source_image'] = $imagePath; 
        $config['create_thumb'] = FALSE;
        $config['new_image'] = 'featured_'.$filename;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 570;
        $config['height']   = 372;
        $this->load->library('image_lib', $config); 
        $this->image_lib->resize();
        $this->image_lib->clear();


    }

    public function resizeThumb($imagePath, $filename){
        $config['image_library'] = 'gd2';
        $config['source_image'] = $imagePath; 
        $config['create_thumb'] = FALSE;
        $config['new_image'] = 'thumb_'.$filename;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 180;
        $config['height']   = 135;
        $this->load->library('image_lib', $config); 
        $this->image_lib->resize();
        $this->image_lib->clear();


    }

Upvotes: 3

Views: 3421

Answers (2)

Nil'z
Nil'z

Reputation: 7475

You need to load the library only once but initialize the library twice with different configs. E.g. :

$this->load->library('image_lib');

/* size 64*72 for comments */
$configThumb = array();  
$configThumb['image_library']   = 'gd2';  
$configThumb['create_thumb']    = TRUE;
$configThumb['new_image']       = './profile_images/thumbs/';  
$configThumb['maintain_ratio']  = TRUE;
$configThumb['width']           = 64;  
$configThumb['height']          = 72;
$configThumb['thumb_marker']    = "";
//$this->load->library('image_lib');
/* size 64*72 for comments */

/* size 167*167 for profile page */
$configThumbMedium = array();  
$configThumbMedium['image_library']   = 'gd2';  
$configThumbMedium['create_thumb']    = TRUE;
$configThumbMedium['new_image']       = './profile_images/medium/';  
$configThumbMedium['maintain_ratio']  = TRUE;
$configThumbMedium['width']           = 167;  
$configThumbMedium['height']          = 167;
$configThumbMedium['thumb_marker']    = "";
/* size 167*167 for profile page */

if(!$this->upload->do_upload('image')){
    return 0;
}

$uploadedDetails    = $this->upload->data();
if($uploadedDetails['is_image'] == 1){
    $this->image_lib->initialize($configThumb);
    $this->image_lib->resize();
    $this->image_lib->initialize($configThumbMedium);
    $this->image_lib->resize();
}

Hope it helps.

Upvotes: 6

umefarooq
umefarooq

Reputation: 4574

hi load library first and use $this->image_lib->initialize($config) to pass config and also in new_image config pass path with new image name.

$this->load->library('image_lib');     
$config['new_image'] = 'image_dir/thumb_'.$filename
//ALL your other configs
$this->image_lib->initialize($config)

Upvotes: 2

Related Questions