从中心PHP裁剪图像

Jun*_*une 12 php image-processing

我想从200*130大小的中心裁剪图像,要裁剪的图像可能会有所不同,如果图像较小我们不会裁剪它我知道如何在这个部分我可以检查高度和图像但从图像的中间进入裁剪的东西当我无法弄清楚如何保持中心作为裁剪点而不是向外裁剪它

Phi*_*hil 33

从版本4.3.6开始,GD捆绑了所有PHP安装,所以很有可能,你有它.

以下是您需要采取的步骤......

  1. 使用GD imagecreatefrom*()功能之一创建图像资源.您使用的那个取决于您正在处理的图像类型
  2. 使用imagesx()和确定图像尺寸imagesy()
  3. 使用以下算法确定裁剪坐标并使用裁剪 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)


tot*_*dli 5

具有可配置对齐方式的图像裁剪

这是函数(称为cropAlign)的本机实现,该函数可以将图像裁剪为给定的宽度和高度,并与 9个标准点(4个边,4个角,1个中心)对齐

对准点

只要传递图像,作物的所需尺寸,并且在所述两个轴的对齐(可以使用leftcenterrighttopmiddlebottom从轴线irregardless)的cropAlign功能。

规格

描述

cropAlign(resource $image, int $width, int $height, string $horizontalAlign = 'center', string $verticalAlign = 'middle')
Run Code Online (Sandbox Code Playgroud)

参量

  • image:图像资源,由图像创建功能之一(例如)返回imagecreatetruecolor()
  • width:最终裁剪图像的宽度。
  • height:最终裁剪图像的高度。
  • horizontalAlign:作物应沿水平轴对齐的位置。可能的值为:left/ topcenter/ middleright/ bottom
  • verticalAlign:作物应沿垂直轴对齐的位置。可能的值为:left/ topcenter/ middleright/ 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)

输出#1

cropAlign($im, 300, 150, 'left', 'top')
Run Code Online (Sandbox Code Playgroud)

输出#2

cropAlign($im, 1000, 250, 'right', 'middle')
Run Code Online (Sandbox Code Playgroud)

输出#3

  • 真的很棒,更有用 (2认同)