在 PHP 图像中裁剪图像复制重新采样 - 挤压而不是裁剪



我正在使用jCrop来裁剪图像。这段代码以前在另一个页面中工作,但是当我为另一个页面实现它时,它似乎正在播放。

当我运行它时,代码会描绘原始图像,将其压扁,然后将整个图像放入我打算裁剪的框中。

这些值以 $_POST 值正确返回 jCrop。

$origURL=$_POST['CelebrityPicURL'];
$x = $_POST['x'];
$y = $_POST['y'];
$w = $_POST['w'];
$h = $_POST['h'];
$targ_w = $targ_h = 300;
$jpeg_quality = 90;
$img_r = imagecreatefromjpeg($origURL);
$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']);

imagejpeg($dst_r,$origURL,$jpeg_quality);
您可以

简单地使用imagecopy() .像...

$dst_r = imagecreatetruecolor((int) $w, (int) $h);
imagecopy($dst_r, $img_r, 0, 0, (int) $x, (int) $y, (int) $w, (int) $h);

当然,您还需要检查越界条件并适当地处理它们。不确定为什么要设置和/或使用$targ_w$targ_h,如果您从$_POST数组中获取裁剪数据。

最新更新