2017-07-04 139 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