GD与版本4.3.6及更高版本的所有PHP安装捆绑在一起,因此您很有可能拥有它。
这是您需要采取的步骤...
- 使用GD
imagecreatefrom*()
函数之一创建图像资源。您所使用的图像取决于要处理的图像类型 - 使用
imagesx()
和imagesy()
确定图像尺寸 - 使用以下算法确定裁切坐标,然后使用
imagecopy()
确定裁切坐标
查找作物坐标
$width = imagesx($img);
$height = imagesy($img);
$centreX = round($width / 2);
$centreY = round($height / 2);
$cropWidth = 200;
$cropHeight = 130;
$cropWidthHalf = round($cropWidth / 2); // could hard-code this but I'm keeping it flexible
$cropHeightHalf = round($cropHeight / 2);
$x1 = max(0, $centreX - $cropWidthHalf);
$y1 = max(0, $centreY - $cropHeightHalf);
$x2 = min($width, $centreX + $cropWidthHalf);
$y2 = min($height, $centreY + $cropHeightHalf);
随意使用我的图像处理类,它应该使某些方面更加容易-https: //gist.github.com/880506
$im = new ImageManipulator('/path/to/image');
$centreX = round($im->getWidth() / 2);
$centreY = round($im->getHeight() / 2);
$x1 = $centreX - 100;
$y1 = $centreY - 65;
$x2 = $centreX + 100;
$y2 = $centreY + 65;
$im->crop($x1, $y1, $x2, $y2); // takes care of out of boundary conditions automatically
$im->save('/path/to/cropped/image');
0
我想从中心以200 * 130的大小来裁剪图像,要裁剪的图像可能会有所不同,如果图像较小,我们将不裁剪它,我知道如何在此部分可以检查图像的高度和图像,但是有点从图像的中间切入到裁切的东西中我无法弄清楚如何保持中心为裁切点,而不是向外裁切