Codeigniter’s image manipulation class enables you to make the routine; crop, resize, rotate and watermaking tasks painless. The general flow is:

Set the options you want but adding them to a config array:

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;

Load the image array with your settings:

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

Then call the method you need, the settings you passed in earlier will be used for the resize:


$this->image_lib->resize();

If you want to carry out more than one action; process multiple images, crop then resize an image etc. You’ll need to reinitialize image_lib with any new settings. For example if you wanted to resize an image into a small and medium thumbnail you may have the settings:

$config['width'] = 75;
$config['height'] = 50;

for your small thumb then want to use:

$config['width'] = 150;
$config['height'] = 100;

for your medium. After resigning the $config variables call:

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

to get image_lib to take in your new settings. So your code might look something like this:

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['new_image'] = '/path/to/image/mypic_small.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->image_lib->resize();
$config['new_image'] = '/path/to/image/mypic_medium.jpg';
$config['width'] = 150;
$config['height'] = 100;
$this->image_lib->initialize($config);
$this->image_lib->resize();

Advertisements