CodeIgniter Resize and crop using axis

I have an image trimmer in my project in CodeIgniter that visits images like picresize.com (I use jCrop ). It works fine with the vanilla code below:

<?php $save_to = $this->config->item('images_gallery_thumb_folder').$data['photo_image']; $targ_w = $this->config->item('gallery_thumb_width'); $targ_h = $this->config->item('gallery_thumb_height'); $src = $this->config->item('images_gallery_folder').$data['photo_image']; $types = array(1 => 'gif', 'jpeg', 'png'); list($width,$height,$type) = getimagesize($src); switch ($types[$type]) { case 'jpeg': $img_r = imagecreatefromjpeg($src); break; case 'gif': $img_r = imagecreatefromgif($src); break; case 'png': $img_r = imagecreatefrompng($src); break; default: $img_r = imagecreatefromjpeg($src); break; } $dst_r = ImageCreateTrueColor($targ_w,$targ_h ); imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], $targ_w,$targ_h,$_POST['w'],$_POST['h']); switch ($types[$type]) { case 'jpeg': imagejpeg($dst_r, $save_to, 90); //90 = jpeg quality break; case 'gif': imagegif($dst_r, $save_to); break; case 'png': imagepng($dst_r, $save_to); break; default: imagejpeg($dst_r, $save_to, 90); //90 = jpeg quality break; } imagedestroy($dst_r); ?> 

But I want to do this with CodeIgniter.

This is what I came up with:

 <?php $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(); $this->image_lib->crop(); ?> 

The fact is that it drops from a position, but it does not change (I think that I have established a large yield area). This is only a crop from this position.

I also use image_moo in the project, but I also failed.

Edit: In Image_moo, here is the code I've come to so far:

 $this->image_moo ->load($src) ->crop($_POST['x'],$_POST['y'],($_POST['x']+$_POST['w']),($_POST['y']+$_POST['h'])) ->resize($targ_w,$targ_h) ->save($save_to,true); 

The fact is that when I use the resize parameter, it completely ignores the cropping line and resizes the whole image. If I resized before and later call the crop, it just fails. I can overcome this with two image_moo calls that I would not want.

This also does not work:

 $this->image_moo ->load($src) ->crop($_POST['x'],$_POST['y'],($_POST['x']+$_POST['w']),($_POST['y']+$_POST['h'])) //->resize($targ_w,$targ_h) ->save($save_to,true) ->resize($targ_w,$targ_h) ->save($save_to,true); 

For example: this way it works:

 $this->image_moo ->load($src) ->crop($_POST['x'],$_POST['y'],($_POST['x']+$_POST['w']),($_POST['y']+$_POST['h'])) //->resize($targ_w,$targ_h) ->save($save_to,true); $this->image_moo ->load($save_to) ->resize($targ_w,$targ_h) ->save($save_to,true); 

So, how do I resize + crop with given x / y offsets using the CodeIgniter (or image_moo) method with one call to image_moo or CI image_lib?

You should probably ask why I call him twice. Well, PQ is important, and I'm worried that calling it twice reduces image quality.

Thanks in advance,

+4
source share
1 answer

Your Codeignign code is fine, except for one piece of logic: you work twice and go to the same image file, so your output file is overwritten by the last change to the original file.

I believe this is a limitation of the CI Image_Lib class, since each operation is performed separately - it is impossible to "resize and crop" at a time.

You need to reinitialize the Image_Lib class between each action to make sure that the next action is called in 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 can also use two different configuration 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 can create a copy of the image file first, and then work with it in every call to the lib image class, while still saving the trouble of reinitializing with the 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(); 
+8
source

Source: https://habr.com/ru/post/1447701/


All Articles