Palette images and transparency

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Palette images and transparency

Postby bonzairob » Sat Jul 09, 2011 2:21 pm

Hey there, I'm having trouble with what should be a simple process: Converting palette PNGs to (palette-only) GIFs.

There seem to be craploads of problems to do with resizing and rotating them, but I haven't come across anythong like my current problem: The alpha is off by one pixel, in the x axis only.

Here's the code:
Syntax: [ Download ] [ Hide ]
$im = imagecreatefrompng($_FILES['uploadedfile']['tmp_name']); 
$sizes = getimagesize($_FILES['uploadedfile']['tmp_name']);
$imtc = imagecreatetruecolor($sizes[0], $sizes[1]);
$transparent = imagecolorallocate($imtc, 255, 0, 255);
imagefilledrectangle($imtc, 0, 0, $sizes[0], $sizes[1], $transparent);
imagecopy($imtc, $im, 0, 0, 0, 0, $sizes[0], $sizes[1]);
imagecolortransparent($imtc, $transparent); //files output here are not transparent at all
imagetruecolortopalette($imtc, false, 32);
imagegif($imtc, 'result.gif'); //this file's alpha is correct other than being x+1.

And the images (fake pokemon sprites, haha):
Input: Image
Output: Image

This seems to be a byproduct of the imagetruecolortopalette function as noted above.

Could I alter the pixel data itself? Would it matter? Thanks a lot for any help!
Forum Newbie
Posts: 1
Joined: Sat Jul 09, 2011 2:13 pm

Re: Palette images and transparency

Postby McInfo » Mon Jul 11, 2011 10:19 pm

The GIF seems to be created correctly with this line removed.
Syntax: [ Download ] [ Hide ]
imagetruecolortopalette($imtc, false, 32);

I don't see any benefit to calling the function.
User avatar
DevNet Resident
Posts: 1531
Joined: Wed Apr 01, 2009 1:31 pm

Return to

Who is online

Users browsing this forum: No registered users and 2 guests