【问题标题】:imagecrop() alternative for PHP < 5.5PHP < 5.5 的 imagecrop() 替代方案
【发布时间】:2014-12-30 14:45:44
【问题描述】:

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

Awn.. 但请不要使用 imagecrop() black line bug。 :p

【问题讨论】:

    标签: gd php-5.4 php-5.5


    【解决方案1】:

    这应该是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;
    }
    

    用法:

    $img = mycrop($img, ['x' => 10, 'y' => 10, 'width' => 100, 'height' => 100]);
    

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

    【讨论】:

    • 显然很完美,但因为这个问题不是我的问题,所以我会在标记你的答案之前先通过解决方案。
    • 我会将您的答案标记为已接受,但使用我自己的标准,因为此解决方案不适用于我的问题,而且相关人员没有给我正确的反馈。我代表这个人感谢你。
    猜你喜欢
    • 2014-04-20
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 2014-06-06
    相关资源
    最近更新 更多