【发布时间】:2012-08-28 20:29:55
【问题描述】:
我正在拍摄源图像并尝试根据用户选择对其进行裁剪。虽然它总是留下一个黑色的空白空间,但我假设它与我以错误顺序使用的参数有关。在查看了 imagecopyresampled() 的 php 文档之后,我仍然没有找到一种方法来完成这项工作。
这就是我所拥有的。我在示例中使用了硬编码值,但每个 $targ_ 变量会因每个图像而变化。
$targ_w = 183; // width of cropped selection
$targ_h = 140; // height of cropped selection
$targ_x = 79; // x-coordinate of cropped selection
$targ_y = 59; // y-coordinate of cropped selection
$targ_x2 = 263; // x-coordinate of 2nd point (end of selection)
$targ_y2 = 199; // y-coordinate of 2nd point (end of selection)
$src = '/uploads/test.png';
$img_r = imagecreatefrompng($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$targ_x,$targ_y,$targ_x2,$targ_y2,$targ_w,$targ_h);
$newFileName = '/uploads/test2.png';
imagepng($dst_r, $newFileName, 9);
【问题讨论】:
-
如果我走错了路,请纠正我,但您的变量标识符建议您在需要尺寸的地方使用坐标。顺序为目标坐标 - 源坐标 - 目标维度 - 源维度
-
它们是坐标,但从技术上讲,它们不是尺寸,因为我正在裁剪选择的 x 和 y?
-
因为应该保留裁剪区域的尺寸(比例尺也是如此),所以目标尺寸和源(=裁剪区域)尺寸必须相同。请参阅下面的答案。
标签: php image-processing crop