使用 CodeIgniter 和 Cropper 进行图像裁剪

Image Cropping using CodeIgniter and Cropper

我正在使用 Cropper 作为 GUI 来裁剪图像。 Cropper 为我提供了从顶部和左侧开始的 X 和 Y 位置以及图像的最终宽度和高度。我将这个数字传递给隐藏的输入字段。 在 CodeIgniter 中,我使用 imagemagick 通过以下代码裁剪图像:

$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 使用这样的代码

$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);

基本上有一行:-crop 100x500+10+10。 此行在 4 轴裁剪图像:

  1. 裁剪:X 轴距左侧 10 像素
  2. 裁剪:Y 轴距顶部 10 像素
  3. 裁剪:X 轴距右侧 $width-100
  4. 裁剪:Y 轴距离底部 $height-500

此外,我将新维度存储在我的数据库中:

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

裁剪后我可以比较文件的尺寸和数据库中的值。除非我不改变我的作物的长宽比,否则这些值是相同的。 当我更改宽高比时,图像会以旧的宽高比裁剪。 我不知道为什么。

如果您需要更多代码,请告诉我。

请像这样更改您的配置文件

$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();

了解更多详情Ckick here并查看示例