水平翻转图像(Flip an Image horizontally)

编程入门 行业动态 更新时间:2024-10-27 08:32:28
水平翻转图像(Flip an Image horizontally)

我需要翻转图像,使角色朝向正确的方向。 这需要像他们所说的那样“即时”完成。

我遇到的问题是,使用Gif图像,我似乎失去了透明度。 (背景变白)

下面是代码:(或者有人可以给我一个很好的例子)

$img = imagecreatefromgif("./unit.gif"); $size_x = imagesx($img); $size_y = imagesy($img); $temp = imagecreatetruecolor($size_x, $size_y); imagecolortransparent($img, imagecolorallocate($img, 0, 0, 0)); imagealphablending($img, false); imagesavealpha($img, true); $x = imagecopyresampled($temp, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y); if ($x) { $img = $temp; } else { die("Unable to flip image"); } header("Content-type: image/gif"); imagegif($img); imagedestroy($img);

I need to flip an image so that a character faces in the right direction. This needs to be done "on the fly' as they say.

The issue I am having is that with Gif images, I seem to lose the transparency. (The background goes white)

Below is the code: (Alternatively someone could send me to a good example)

$img = imagecreatefromgif("./unit.gif"); $size_x = imagesx($img); $size_y = imagesy($img); $temp = imagecreatetruecolor($size_x, $size_y); imagecolortransparent($img, imagecolorallocate($img, 0, 0, 0)); imagealphablending($img, false); imagesavealpha($img, true); $x = imagecopyresampled($temp, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y); if ($x) { $img = $temp; } else { die("Unable to flip image"); } header("Content-type: image/gif"); imagegif($img); imagedestroy($img);

最满意答案

不应该这样:

imagecolortransparent($img, imagecolorallocate($img, 0, 0, 0)); imagealphablending($img, false); imagesavealpha($img, true);

......是这样的:

imagecolortransparent($temp, imagecolorallocate($img, 0, 0, 0)); imagealphablending($temp, false); imagesavealpha($temp, true);

请注意,您应该为您创建的$ temp图像调用这些函数,而不是源图像。

Shouldn't this:

imagecolortransparent($img, imagecolorallocate($img, 0, 0, 0)); imagealphablending($img, false); imagesavealpha($img, true);

...be this:

imagecolortransparent($temp, imagecolorallocate($img, 0, 0, 0)); imagealphablending($temp, false); imagesavealpha($temp, true);

Note you should be calling these functions for the $temp image you have created, not the source image.

更多推荐

本文发布于:2023-07-05 07:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034838.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   水平   Flip   Image   horizontally

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!