CodeIgniter Resizing And Cropping With Axis CodeIgniter Resizing And Cropping With Axis codeigniter codeigniter

CodeIgniter Resizing And Cropping With Axis


Your given Codeigniter code is OK except for one piece of logic: You're operating on and outputting to the same image file twice, so your output file gets overwritten by the last change to the original file.

I believe this is a limitation of CI's Image_Lib class, as each operation is done separately - there is no way to "resize and crop" in one go.

You have to reinitialize the Image_Lib class between each action to make sure the next action gets called on the output file of the last operation.

$img_config = array(    'source_image'      => $src,    'new_image'         => $save_to,    'maintain_ratio'    => false,    'width'             => $targ_w,    'height'            => $targ_h,    'x_axis'            => $_POST['x'],    'y_axis'            => $_POST['y']);$this->load->library('image_lib', $img_config);$this->image_lib->resize();// Now change the input file to the one that just got resized// See also $this->image_lib->clear()$img_config['source_image'] = $save_to;$this->image_lib->initialize($img_config); $this->image_lib->crop();

You could also use two different config arrays:

$this->load->library('image_lib');$this->image_lib->initialize(array(    'source_image'      => $src,    'new_image'         => $save_to,    'maintain_ratio'    => false,    'width'             => $targ_w,    'height'            => $targ_h,));$this->image_lib->resize();$this->image_lib->clear();$this->image_lib->initialize(array(    'source_image'      => $save_to,    'x_axis'            => $_POST['x'],    'y_axis'            => $_POST['y']));$this->image_lib->crop();

Alternatively, you could create the copy of the image file first and then operate on that in each call to the image lib class, saving you the hassle of reinitializing with a new source_image:

copy($src, $save_to);$this->load->library('image_lib', array(    'source_image'      => $save_to,    'maintain_ratio'    => false,    'width'             => $targ_w,    'height'            => $targ_h,    'x_axis'            => $_POST['x'],    'y_axis'            => $_POST['y']));$this->image_lib->resize();$this->image_lib->crop();