Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 470
Soriyyx Avatar asked Mar 30 '13 19:03

Soriyyx


1 Answers

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.

like image 65
Jon Avatar answered Sep 24 '22 19:09

Jon