Re: GD and Converting a Transparent PNG-8 to a Transparent GIF

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thanks for the reply

I tried the example (below) from the link and still get a gif with white background. Maybe, you can only do this with PNG-24s...not PNG-8s?
Anyone else know if this can work?


# Convert the PreExisting PNG Image to a GIF
$img = imagecreatefrompng($pngPath);

$gifAbsolutePath = PROJECT_ROOT_DIR.'/'.$gifDir.'/'.$gif;								
						
$transparentColor = imagecolorallocate($img, 0xfe, 0x3, 0xf4 );
//$ditherHistory = array(); //stores number of times it was transparent
$height = imagesy($img);
$width = imagesx($img);
      		
for($x = 0; $x < $width; $x++):
for($y = 0; $y < $height; $y++):
$alpha = (imagecolorat($img,$x,$y) & 0x7F000000) >> 24;//127 is completely TRANSPARENT, 0 opaque
//DITHER!
if($alpha>3&&(
$alpha >=127-3 ||
(rand(0,127))>=(127-$alpha)
)){
imagesetpixel($img,$x,$y,$transparentColor);
}

endfor;
endfor;
     		
imagecolortransparent($img, $transparentColor);
							
# Save the Image as a GIF in the directory $gifDir
imagegif($img, $gifAbsolutePath);
			
imagedestroy($img);


On Jan 18, 2009, at 4:46 PM, Jochem Maas wrote:

Graham Anderson schreef:
Hi

I am having problems getting GD to convert a transparent PNG-8 to a
transparent GIF
The below WILL produce a GIF...but leaves a white background

# Convert the PreExisting PNG Image to a GIF
$img = imagecreatefrompng($pngPath);

# Set the GIF to be transparent: Does not seem to work
$trans_color = imagecolortransparent($img );
$trans_index = imagecolorallocate($img, $trans_color['red'],
$trans_color['green'], $trans_color['blue'] );
imagecolortransparent($img, $trans_index );

I can't see the above code doing much useful. the problem is somewhat
more involved, png uses an alpha channel to determine how much transparency a given pixel has, with gif a pixel is either transparent or not, essentially
a given colour in the gif's palette is marked as transparent and all
pixels with that color are therefore shown as transparent.

try the code here (no idea how well it works):

	http://demo.pixelsandpages.com/php-tests/images/test.html

and/or dig into the user comments here:

	http://php.net/imagecolortransparent


# Save the Image as a GIF in the directory
imagegif($img, $gifAbsolutePath);


Is there another way that works?

Many thanks in advance

G




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux