用于PHP <5.5的imagecrop()替代方案

Bru*_*sto 8 gd php-5.4 php-5.5

一个由好奇心驱动的简单问题,可能是一个复杂的答案:是否可以通过组合其他GD函数来模拟早期版本中的新PHP 5.5 imagecrop(),如5.4.

Awn ..但是没有imagecrop()线虫,请.:p

tim*_*ton 18

这应该是一个简单的替代imagecrop()(没有错误......):

function mycrop($src, array $rect)
{
    $dest = imagecreatetruecolor($rect['width'], $rect['height']);
    imagecopy(
        $dest,
        $src,
        0,
        0,
        $rect['x'],
        $rect['y'],
        $rect['width'],
        $rect['height']
    );

    return $dest;
}
Run Code Online (Sandbox Code Playgroud)

用法:

$img = mycrop($img, ['x' => 10, 'y' => 10, 'width' => 100, 'height' => 100]);
Run Code Online (Sandbox Code Playgroud)

请注意,该错误显然已在PHP 5.6.12中修复.