Posted by & filed under PHP/CodeIgniter.

I found the documentation on using CodeIgniter’s Image Manipulation class (image_lib) in a loop, creating multiple sizes of a graphic a little confusing and hard to get a handle on, so the following is an example of how to do this…

I found the documentation on using CodeIgniter’s Image Manipulation class (image_lib) in a loop, creating multiple sizes of a graphic a little confusing and hard to get a handle on, so the following is an example of how to do this… This example assumes that you are also using CodeIgniter’s File Uploading Class to upload the image you wish to resize (if this isn’t the case, you can replace the “uploaddata” variable with whatever you want. I opted not to use the “create_thumb” parameter, as I want to keep my file names identical.

The big catch for me was understanding that, as far as I understand it, the initialize function is used each time through a loop iteration to read in a new config, and that while you can provide a config when the class is initially loaded (“$this->load->library”), this should only be done once. I’m not completely clear on whether both clearing out the “$config” array and running “$this->image_lib->clear()” is necessary, but this worked for me.


// uploaddata contains an array of information about the file you just uploaded using
// CodeIgniter's File Uploading Class (http://codeigniter.com/user_guide/libraries/file_uploading.html)
$uploaddata = $this->upload->data();

// clear config array
$config = array();

// create resized image
$config['image_library'] = 'GD2';
$config['source_image'] = $uploaddata['full_path'];
$config['new_image'] = '/path/to/new/image';
$config['create_thumb'] = false;
$config['maintain_ratio'] = true;
$config['width'] = ;
$config['height'] = ;

$this->load->library('image_lib', $config);
$this->image_lib->resize();

//$this->image_lib->display_errors();

$this->image_lib->clear();

$config = array();

// create thumb
$config['image_library'] = 'GD2';
$config['source_image'] = $uploaddata['full_path'];
$config['new_image'] = '/another/path/to/thumb',
$config['create_thumb'] = false;
$config['maintain_ratio'] = true;
$config['width'] = ;
$config['height'] = ;

$this->image_lib->initialize($config);
$this->image_lib->resize();

//$this->image_lib->display_errors();

  • http://victorelu.com Victor

    Thanks for the article – I couldn’t really figure out how to reinitialize the configuration for the uploaded file. Cheers!

  • Nayan

    Thanks a lot. I was doing the same thing in wrong way. I have to resize two images one after one. But it was not working. I spent almost 2 days behind this. Finally I came to know from your post that I’ve to call the initialize and clear functions.

    Thanks again man. Hope you’ll continue to serve the community

    • NetMusician Support

      I’m glad you were able to sort this out, it took me some time to figure out too!

  • muhammed misbah

    thanks very much…. but i dont want to store original image… pls give me reply

  • kaka

    thanks that is working….

  • Erkan

    thank you very much.. you saved me a big trouble..