toiglicher
toiglicher

Reputation: 59

Resize an image and then crop it with Codigniter

Hi i'm using the Codigniter image manipulation class in order to create a thumbnail with a fixed size So i wont first to resize it (So when i crop it i get a large peace of the image) And then crop it The resize works But the crop dont

Here is the function I've created

function _generate_thumbnail($filename)
{
$config['image_library']     = 'gd2';
$config['source_image']      = './project_pics/big/'.$filename;
$config['new_image']         = './project_pics/resize/'.$filename;
$config['maintain_ratio']    = TRUE;
$config['width']             = 650;

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

$this->image_lib->clear();

$config['image_library'] = 'gd2';
$config['source_image']  = './project_pics/resize/'.$filename;
$config['new_image']     = './project_pics/crop/'.$filename;
$config['width']         = 650;
$config['height']        = 450;
$config['x_axis']        = 0;
$config['y_axis']        = 0;

$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->crop();   
}

Upvotes: 1

Views: 773

Answers (1)

Satish Saini
Satish Saini

Reputation: 2968

There are some limitation of using Codeigniter's image_lib library. You can't do "resize and crop" in one go with this lib.

You will have to reintialize image_lib between each action like this:

function _generate_thumbnail($filename)
{
 $config1 = $config2 = array();

 $config1['image_library']     = 'gd2';
 $config1['source_image']      = './project_pics/big/'.$filename;
 $config1['new_image']         = './project_pics/resize/'.$filename;
 $config1['maintain_ratio']    = TRUE;
 $config1['width']             = 650;

 $this->load->library('image_lib');
 $this->image_lib->initialize($config1);
 $this->image_lib->resize();

 $this->image_lib->clear();

 $config2['image_library'] = 'gd2';
 $config2['source_image']  = './project_pics/resize/'.$filename;
 $config2['new_image']     = './project_pics/crop/'.$filename;
 $config2['width']         = 650;
 $config2['height']        = 450;
 $config2['x_axis']        = 0;
 $config2['y_axis']        = 0;

 $this->image_lib->initialize($config2);
 $this->image_lib->crop();   
}

Upvotes: 1

Related Questions