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