【问题标题】:PHP GD imagecopyresampled() and flip it horizontalPHP GD imagecopyresampled() 并将其水平翻转
【发布时间】:2011-10-13 21:28:41
【问题描述】:

我正在使用 imagecopyresampled() 从另一个 PNG 图像渲染 PNG 图像。现在我想让图像的某些部分水平翻转,所以我尝试了这个:

//horizontal
$src_x     = $width - 1;
$src_width = -$width;

imagecopyresampled(
    $imgdest, $imgsrc, 0, 0, $src_x, $src_y , $width, $height
    , $src_width, $src_height
);

取自a user-comment from the PHP Manual

在我的情况下它不起作用(我将很多部分从原始图像复制到新图像),而是复制另一块图像。有没有人可以解决这个问题?

【问题讨论】:

    标签: php image gd image-manipulation


    【解决方案1】:

    我知道这有点晚了,但我自己也在寻找这个解决方案,只是找到了所需的代码......

    function image_flip($img, $type=''){
        $width  = imagesx($img);
        $height = imagesy($img);
        $dest   = imagecreatetruecolor($width, $height);
        switch($type){
            case '':
                return $img;
            break;
            case 'vert':
                for($i=0;$i<$height;$i++){
                    imagecopy($dest, $img, 0, ($height - $i - 1), 0, $i, $width, 1);
                }
            break;
            case 'horiz':
                for($i=0;$i<$width;$i++){
                    imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
                }
            break;
            case 'both':
                for($i=0;$i<$width;$i++){
                    imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
    
                }
                $buffer = imagecreatetruecolor($width, 1);
                for($i=0;$i<($height/2);$i++){
                    imagecopy($buffer, $dest, 0, 0, 0, ($height - $i -1), $width, 1);
                    imagecopy($dest, $dest, 0, ($height - $i - 1), 0, $i, $width, 1);
                    imagecopy($dest, $buffer, 0, $i, 0, 0, $width, 1);
                }
                imagedestroy($buffer);
            break;
        }
        return $dest;
    }
    

    【讨论】:

    • 谢谢你的回答卢克。我不知道如何使用它,因为我的图像是从大量图像中创建的,你的函数看起来像是翻转了整个图像。我需要的是要翻转的图像的一部分。喜欢这个部分:imagecopy($output, $input, 0,8, 44,20, 4,12);
    • 基本上,这段代码是从源图像逐行或逐列复制像素到目标,同时反转 x、y 或两个轴
    【解决方案2】:

    好吧,在我自己找到答案这么多年之后,我只想让其他人知道。

    很简单,例如:

    代替:

    imagecopy($output, $input, 8, 20, 4, 20, 4, 12)
    

    我会使用:

    imagecopyresampled($output, $input, 8, 20, (8 - 1), 20, 4, 12, 0 - 4, 12);
    

    这会将图像的一部分水平翻转。

    【讨论】:

      【解决方案3】:

      我用那个:

       imageflip ( resource $image , int $mode ) : bool
      

      https://www.php.net/manual/es/function.imageflip.php

      【讨论】:

        猜你喜欢
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-21
        • 2011-08-11
        • 2011-12-08
        • 1970-01-01
        相关资源
        最近更新 更多