简体   繁体   中英

Image Cropping using CodeIgniter and Cropper

I'm using Cropper as a GUI to crop an image. Cropper gives me the X and Y position from top and left and the resulting width and height of the image. I'm passing this numbers to hidden input fields. In CodeIgniter I'm using imagemagick to crop the image with this code:

$this->load->library('image_lib');
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/Applications/MAMP/Library/bin/';
$config['source_image'] = "upload/".$data['image']['file_name'];
$config['x_axis'] = $post['dataX'];
$config['y_axis'] = $post['dataY'];
$config['width'] = $post['dataWidth'];
$config['height'] = $post['dataHeight'];

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

if ( ! $this->image_lib->crop()) {
  echo $this->image_lib->display_errors();
}

ImageMagick uses a code like this

$cmd = $this->library_path.' -quality '.$this->quality;
/* ... */
if ($action === 'crop')
{
    $cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis;
}
/* ... */
$cmd .= ' '.escapeshellarg($this->full_src_path).' '.escapeshellarg($this->full_dst_path).' 2>&1';
/* ... */
@exec($cmd, $output, $retval);

Basically there is a line: -crop 100x500+10+10. This line crops the image at 4 axis:

  1. crop: X-Axis at 10px from the left
  2. crop: Y-Axis at 10px from the top
  3. crop: X-Axis at $width-100 from the right
  4. crop: Y-Axis at $height-500 from the bottom

Additionally I store the new dimensions in my database:

$this->db->where('id', $id);
$this->db->update("images", array(
  'width' => $post['dataWidth'],
  'height' => $post['dataHeight']
));

After a crop I can compare the dimensions of the file and the values in the database. Unless I don't change the aspect ratio of my crop the values are the same. When I change the aspect ratio, the Image crops in the old aspect ratio. I'm not able to figure out why.

If you need more code, please tell me.

Please Change in your config file Like This

$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 Ckick here and see the example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM