Jun*_*une 12 php image-processing
我想从200*130大小的中心裁剪图像,要裁剪的图像可能会有所不同,如果图像较小我们不会裁剪它我知道如何在这个部分我可以检查高度和图像但从图像的中间进入裁剪的东西当我无法弄清楚如何保持中心作为裁剪点而不是向外裁剪它
Phi*_*hil 33
从版本4.3.6开始,GD捆绑了所有PHP安装,所以很有可能,你有它.
以下是您需要采取的步骤......
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);
Run Code Online (Sandbox Code Playgroud)
随意使用我的图像处理类,它应该使一些方面更容易 - 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');
Run Code Online (Sandbox Code Playgroud)
这是函数(称为cropAlign
)的本机实现,该函数可以将图像裁剪为给定的宽度和高度,并与 9个标准点(4个边,4个角,1个中心)对齐。
只要传递图像,作物的所需尺寸,并且在所述两个轴的对齐(可以使用left
,center
,right
或top
,middle
,bottom
从轴线irregardless)的cropAlign
功能。
描述
Run Code Online (Sandbox Code Playgroud)cropAlign(resource $image, int $width, int $height, string $horizontalAlign = 'center', string $verticalAlign = 'middle')
参量
image
:图像资源,由图像创建功能之一(例如)返回imagecreatetruecolor()
。width
:最终裁剪图像的宽度。height
:最终裁剪图像的高度。horizontalAlign
:作物应沿水平轴对齐的位置。可能的值为:left
/top
,center
/middle
,right
/bottom
。verticalAlign
:作物应沿垂直轴对齐的位置。可能的值为:left
/top
,center
/middle
,right
/bottom
。返回值
成功或
FALSE
失败时返回裁剪的图像资源。这来自imagecrop()
。
cropAlign(resource $image, int $width, int $height, string $horizontalAlign = 'center', string $verticalAlign = 'middle')
Run Code Online (Sandbox Code Playgroud)
function cropAlign($image, $cropWidth, $cropHeight, $horizontalAlign = 'center', $verticalAlign = 'middle') {
$width = imagesx($image);
$height = imagesy($image);
$horizontalAlignPixels = calculatePixelsForAlign($width, $cropWidth, $horizontalAlign);
$verticalAlignPixels = calculatePixelsForAlign($height, $cropHeight, $verticalAlign);
return imageCrop($image, [
'x' => $horizontalAlignPixels[0],
'y' => $verticalAlignPixels[0],
'width' => $horizontalAlignPixels[1],
'height' => $verticalAlignPixels[1]
]);
}
function calculatePixelsForAlign($imageSize, $cropSize, $align) {
switch ($align) {
case 'left':
case 'top':
return [0, min($cropSize, $imageSize)];
case 'right':
case 'bottom':
return [max(0, $imageSize - $cropSize), min($cropSize, $imageSize)];
case 'center':
case 'middle':
return [
max(0, floor(($imageSize / 2) - ($cropSize / 2))),
min($cropSize, $imageSize),
];
default: return [0, $imageSize];
}
}
Run Code Online (Sandbox Code Playgroud)
cropAlign($im, 200, 250, 'center', 'middle')
Run Code Online (Sandbox Code Playgroud)
cropAlign($im, 300, 150, 'left', 'top')
Run Code Online (Sandbox Code Playgroud)
cropAlign($im, 1000, 250, 'right', 'middle')
Run Code Online (Sandbox Code Playgroud)