Soriyyx Soriyyx - 2 months ago 14
PHP Question

imagecolortransparent in PHP not working

I want to change white color in image (http://www.arso.gov.si/vreme/napovedi%20in%20podatki/radar.gif) 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.

<?php
$im = imagecreatefromgif("http://www.arso.gov.si/vreme/napovedi%20in%20podatki/radar.gif");

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

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

imagecolortransparent($im, $white);

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

Jon Jon
Answer

Change:

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

To:

$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.

Comments