2011-07-03 70 views
4

我有一個PNG圖像我想打開並輸出爲GIF圖像。但是,當我這樣做時,透明度會丟失,背景變黑。如果我將圖像作爲PNG輸出,它可以工作,但我特別需要將圖像作爲PNG打開並將其輸出爲GIF。將PNG轉換爲GIF時的黑色背景

這是我到目前爲止有:

<?php 
header("Content-type: image/gif"); 

$new_img = imagecreatefrompng($image); 
imagealphablending($new_img, false); 
imagesavealpha($new_img, true); 
imagegif($new_img); 
?> 

然而,imagepng($ new_img)保存背景透明度,但不輸出爲GIF格式。

+0

哦,如果我在第一個地方創建圖像,並做到這一點,它的工作正常。所以這不是我的瀏覽器。從PNG到GIF的過渡讓它變得混亂起來。 – TheEnigmaMachine

+2

@sombe - 「大多數主流瀏覽器都不支持gif中的透明度」,我認爲自1800年以來,所有瀏覽器都支持GIF中的透明度。 – vcsjones

回答

0

所以我設法得到這個工作。請讓我知道,如果任何人有更好的解決方案:

<?php 
    header("Content-type: image/gif"); 

    $new_img = imagecreatefrompng($image); 
    $background = imagecreatefrompng("background.png"); 
    imagecopyresampled($background, $new_img, 0, 12, 0, 0, 100, 125, 100, 125); 
    $c = imagecolorat($background, 0, 0); 
    imagefilledrectangle($background, 0, 112, 100, 125, $c); 
    imagecolortransparent($background, $c); 
    imagegif($new_img); 
    ?> 
0

嘗試使用此代碼:

<?php 
header("Content-type: image/gif"); 

$new_img = imagecreatefrompng($image); 
$trans_color = imagecolortransparent($new_img); 
$trans_index = imagecolorallocate($new_img, $trans_color['red'], $trans_color['green'], $trans_color['blue']); 
imagecolortransparent($new_img, $trans_index); 
imagegif($new_img); 
?> 
+0

這似乎並不奏效。我認爲主要的問題是從PNG到GIF的過渡,所以也許有一種方法可以將圖像轉換爲GIF,然後在將所有內容輸出到瀏覽器之前混淆透明度。 – TheEnigmaMachine

相關問題