Search code examples
phpphp-gd

PNG file is NOT keeping transparency?


I'll be using these variables throughout:

$ROOTDIR = $_SERVER["DOCUMENT_ROOT"];
$ROOTFILE = "http://www.scottandjessiecooper.com/webtutorials/images/smiley.png";
$NEWFILE = "$ROOTDIR/images/tmp/new_smiley.png";

When I use this function, I have NO problems with transparency

function save_image($root, $saveto){
    copy($root, $saveto);
}
save_image( $ROOTFILE, $NEWFILE ); // root can be file or url

However I NEED to use an IMAGE_RESOURCE so i cam manipulate the ROOTFILE if needed

So i treid this:

if ( file_exists( $NEWFILE ) ) unlink ($NEWFILE);
$image = imagecreatefrompng( $ROOTFILE );
imagepng( $image, $NEWFILE );
imagedestroy( $image );

Now when I use this:

<img src="<?=$NEWFILE?>" />

I lose the transparency. The background goes BLACK!

So I tried outputting the image to make sure it wasn't the saving that caused the problem:

if ( file_exists( $NEWFILE ) ) unlink ($NEWFILE);
$image = imagecreatefrompng( $ROOTFILE );
header('Content-Type: image/png');
imagepng( $image );
imagedestroy( $image );

Still to no avail...

Help?


Solution

  • You need to enabled alpha blending and save alpha. I found this after a 10 sec google search: http://www.php.net/manual/en/function.imagecreatefrompng.php#43024