水平翻转图像(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.
更多推荐
发布评论