使用 CodeIgniter 和 Cropper 进行图像裁剪

Posted

技术标签:

【中文标题】使用 CodeIgniter 和 Cropper 进行图像裁剪【英文标题】:Image Cropping using CodeIgniter and Cropper 【发布时间】:2017-12-28 13:11:53 【问题描述】:

我使用 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 轴上裁剪图像:

    裁剪:X 轴距左侧 10 像素 裁剪:Y 轴距顶部 10 像素 裁剪:X 轴在 $width-100 的右侧 裁剪:Y 轴距底部 $height-500

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

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

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

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

【问题讨论】:

哦,我的错……我只需要设置:$config['maintain_ratio'] = FALSE; 【参考方案1】:

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

$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 并查看示例

【讨论】:

以上是关于使用 CodeIgniter 和 Cropper 进行图像裁剪的主要内容,如果未能解决你的问题,请参考以下文章

cropper.js头像剪裁

java web 网站头像上传处理 (springmvc +bootstrap+cropper)

java web 网站头像上传处理 (springmvc +bootstrap+cropper)

java web 网站头像上传处理 (springmvc +bootstrap+cropper)

cropper.js裁剪图片的使用

cropper 怎么异步初始化