简体   繁体   English

GIF透明背景变成黄色

[英]GIF transparent background turned yellow

I tried several solutions, but nothing work. 我尝试了几种解决方案,但没有任何效果。

$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');

Result: 结果:

在此处输入图片说明

GIF images don't support alpha-channel transparency. GIF图片不支持Alpha通道透明度。

Since you're trying to set an existing image colour to transparent you need to find it in the palette. 由于您尝试将现有的图像颜色设置为透明,因此需要在调色板中找到它。 You can do that with imagecolorexact : 您可以使用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');

Result: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM