我正在拍摄源图像并尝试基于用户选择裁剪它。它总是留下一个黑色的空白空间,我认为它与我使用的参数不正确的顺序有关。在查看php文档中的imagecopyresampled()之后,我仍然想尽一切办法来完成这项工作。imagecopyresampled裁剪黑色条
这是我的。我为示例使用了硬编码值,但每个图像的每个$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);
如果我只是在错误的路径上,请纠正我,但您的变量标识符建议您使用需要维度的坐标。顺序是_dest坐标 - 源坐标 - 目标尺寸 - 源尺寸_ – Andre
它们是坐标,但在技术上不是它们的尺寸,因为我选择的x和y要裁剪? –
由于裁剪区域的尺寸(也是尺度)应该保留,因此目标尺寸和来源(=作物面积)尺寸必须相同。请参阅下面的答案。 – Andre