0
我試圖用一個透明背景以取代GIF/JPEG文件黑色的背景,更改黑色到透明的,因爲它是如此累人由通過PhotoShop的一個手工做這一塊,我認爲嘗試使用PHP 。這是我正在使用的代碼,但它不起作用。在PHP
這是爲什麼?
$image = imagecreatefromgif('items/item_spear06.gif');
imagealphablending($image, true);
$transparentcolour = imagecolorallocate($image, 0,0,0);
imagecolortransparent($image, $transparentcolour);
而我得到的圖像和上一次只是png格式一樣。
更新
$image = imagecreatefromgif('items/sword/iv_sword_refined19.gif');
//get pixel data
$rgb = imagecolorat($image, 10, 10);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$index = imagecolorexact($image, $r, $g, $b);
if ($index === -1) {
$index = imagecolorallocate($image, $r, $g, $b);
}
imagecolortransparent($image, $index);
var_dump($r, $g, $b);
// Save the image
imagepng($image, 'imagecolortransparent.png');
imagedestroy($image);
現在的var_dump回報:int(0) int(0) int(0)
當它應該是int(4) int(2) int(4)
這工作。 :) 謝謝。將在4分鐘內接受。 – zikdaljin 2013-05-13 18:33:07
這很奇怪。這個相同的代碼不適用於其他圖像。爲什麼會這樣? – zikdaljin 2013-05-13 18:40:18
我可能得到了如果錯誤,並且檢查imagecolorallocate上的輸出,如果調色板中沒有空間,它可能會失敗 – hank 2013-05-13 18:41:38