2017-07-04 127 views
0

我尝试了几种解决方案,但没有任何工作。GIF透明背景变成黄色

$srcImage = imagecreatefromgif($_FILES['img']['tmp_name']); 
$bg = imagecolorallocatealpha($srcImage, 0, 0, 0, 127); 
imagecolortransparent($srcImage, $bg); 
imagealphablending($srcImage, false); 
imagesavealpha($srcImage, true); 

imagegif($srcImage, 't.gif'); 

结果:

enter image description here

回答

1

GIF图像不支持alpha通道透明度。

由于您试图将现有的图像颜色设置为透明,所以您需要在调色板中找到它。你可以做到这一点与imagecolorexact

$srcImage = imagecreatefromgif($_FILES['img']['tmp_name']); 
$bg = imagecolorexact($srcImage, 204, 168, 46); // RGB here matches the yellowish colour in your image. 
imagecolortransparent($srcImage, $bg); 

imagegif($srcImage, 't.gif'); 

结果:

enter image description here

+0

的问题是我不知道的背景会变成什么颜色。但我测试了多个透明背景的GIF图片,似乎是黑色的。因此,'imagecolorexact($ image,0,0,0)'将完成这项工作。 – Bacchus