I just want to take PNG-24s (with alpha transparency), and convert them to a GIF with indexed transparency.
In an ideal world, one would think this would be enough to do the trick:
$src = imagecreatefrompng( 'src.png' );
imagegif( $src, 'output.gif' );
Indeed, this gets very close. The soft edges of the PNG24 perfectly resolve into 'jagged' indexed transparency.... however, the area of the image that should be transparent, is being set to black.
I've been able to 'hack' around this by manually setting black to transparent after-the-fact with imagecolortransparent(), but this seems like a very poor general solution, especially if the source image contains pure black pixels, as these will also get set to transparent in the final version. (Even more worryingly, I have seen some PNGs that when converted, the transparent color becomes white instead of black, so perhaps this 'phantom transparent background color' depends on how the PNG was originally saved?)
I have played around extensively with imagesavealpha() and imagealphablending(), but neither seem to help here (unless I am using them incorrectly). I have also tried creating an intermediate, transparent canvas and merging the source image onto it, with no better results. I either merge in truecolor and wind up with what I started with, or merge to a palette and the black background always re-appears.
I would've thought this would be a common task to perform, given IE's notorious inabilty to handle PNG alpha (without the blasted alpha loader filter of course). Does anybody have any example code that will convert a PNG24 to GIF, while properly retaining the fully transparent areas?
Here is a sample PHP page to upload and convert a PNG:
<?php
if( !empty( $_FILES ) && move_uploaded_file( $_FILES['png']['tmp_name'], 'original.png' ) )
{
$src = imagecreatefrompng( 'original.png' );
imagegif( $src, 'output.gif' );
}
?>
<body bgcolor='#abcabc'>
<h1>PNG 2 GIF</h1>
<form method='post' enctype='multipart/form-data'>
<p>PNG File: <input type='file' size='60' name='png' /></p>
<p><input type='submit' value='Convert' /></p>
</form>
<img src='original.png' />
<img src='output.gif' />
</body>
Attached, please find a couple sample PNGs. Thanks a ton in advance, for any help or suggestions on how to proceed here.
-Brian