Mehur Mehur - 1 month ago 22
PHP Question

Codeigniter image cropping is not working

I am using codeigniter 3.1 on localhost.
I am trying to crop images but cropping is not working. Only resize working. So i enabled

Gd2
php extension.

1.

public function do_crop($filename)
{
$this->load->library('image_lib');
$source_path = 'uploads/' . $filename;
$target_path = 'uploads/thumb/'.$filename;

$config = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => FALSE,
'x_axis' => 300,
'y_axis' => 100,
);
$this->image_lib->initialize($config);
}


Image size = 1000X700

The output result is same as original image size
1000X700


2.

public function do_crop($filename)
{
$this->load->library('image_lib');
$source_path = 'uploads/' . $filename;
$target_path = 'uploads/thumb/'.$filename;

$config = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => FALSE,
'width' => 300,
'height' => 300,
'x_axis' => 350,
'y_axis' => 50
);
$this->image_lib->initialize($config);
}


Image size = 1000X700

And the 2nd example only resize (
300x300
) the image but not cropped.

Answer
$config = array(
'source_image' => $upload_path.$image_data['file_name'],
'maintain_ratio' => FALSE,
'width' => 220,
'height' => 150,
'x_axis' => 350,
'y_axis' => 50
);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->crop();

For more details Please See Here For More Info Check Here