Soriyyx Soriyyx - 8 months ago 54
PHP Question

imagecolortransparent in PHP not working

I want to change white color in image ( to transparent. I think that code looks right, there is no error logs but picture stays unchanged. I double checked if color in image is really white and it is. Please help.

$im = imagecreatefromgif("");

imagealphablending($im, false);
imagesavealpha($im, true);

$white = imagecolorallocate($im, 255, 255, 255);

imagecolortransparent($im, $white);

imagegif($im, './image_radar_tran.gif');
<body style="background-color: lightgoldenrodyellow;">
<img src="./image_radar_tran.gif" />

Jon Jon


$white = imagecolorallocate($im, 255, 255, 255);


$white = imagecolorexact($im, 255, 255, 255);

And it will work. The reason is that the color 'white' is already defined in the index of the gif you are using, so you can't allocate a new index for that color. Instead, using imagecolorexact, you get the existing index for the white color to use and can then change it to transparent.