开发者

PHP GD imagecopyresampled() and flip it horizontal

开发者 https://www.devze.com 2023-04-13 01:45 出处:网络
I\'m rendering a PNG image from another PNG image with imagecopyresampled(). Now i want some parts of the image to be flipped horizontal, so i have tried this:

I'm rendering a PNG image from another PNG image with imagecopyresampled(). Now i want some parts of the image to be flipped horizontal, so i have tried this:

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

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

Taken from a user-comment from the PHP Manual.

It does not work in my case (where I 开发者_开发问答copy a lot of pieces from the original image to the new), instead it copies another piece of the image. Does anyone has a solution to this?


I know this is a little late but I was looking for this solution myself too and just found the code needed...

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;
}


Okay so many years after I found the answer myself, so I just wanted to let anybody else know.

It was pretty simple, example:

Instead of:

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

I would use:

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

Which would flip the part of the image horizontal.


I use that:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消