【发布时间】:2012-11-10 17:00:46
【问题描述】:
我在 CodeIgniter 的项目中有一个图像裁剪器,它可以像 picresize.com 那样裁剪图像(我正在使用 jCrop)。它适用于下面给出的香草代码:
<?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);
?>
但我想用 CodeIgniter 的方式来做。
这是我目前想出的:
<?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();
?>
问题是,它从位置裁剪,但它不会调整大小(就像我设置了一个更大的裁剪正方形一样)。它只从给定位置裁剪。
我也在项目中使用image_moo 库,但我也无法成功。
编辑: 在 Image_moo 中,这里是我目前想出的代码:
$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 参数时,它会完全忽略裁剪线并调整整个图像的大小。如果我之前调整大小并稍后调用裁剪,它就会失败。 我可以通过使用两个 image_moo 调用来克服它,这是我不喜欢的。
这也不起作用:
$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);
例如:这样工作:
$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);
那么如何通过调用 image_moo 或 CI image_lib 以 CodeIgniter(或 image_moo)方式调整给定 x/y 偏移量的大小+裁剪?
您可能应该问我为什么担心调用它两次。嗯,PQ 很重要,我很担心,因为调用它两次会降低图像质量。
提前致谢,
【问题讨论】:
-
您必须先进行裁剪,以获得正确的比例,然后再调整大小。在 Codeigniter 中,它们被拆分为单独的函数。
-
是你的裁剪脚本作品......
-
@Jeemusu - 问题是我无法管理它。我应该先制作一个更大的缩略图,然后将其调整为所需的宽度/高度吗?能具体点吗?
-
@AnkurSaxena - 它自己工作,但还不够。不是每个 PHP 安装都有 GD2 等(我不会拥有这个脚本所在的服务器的管理权限,所以。)。 CodeIgniter 在后台管理这一切。这就是我想要 CI 方式的原因之一。另外,如果你问我,在 CI 脚本中包含所有 CI 方式会更好。
-
对不起,我的朋友不介意。我也通过 gd lib 进行图像处理。我没有裁剪代码。
标签: php codeigniter image-processing jcrop