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

Post Reply
bonzairob
Forum Newbie
Posts: 1
Joined: Sat Jul 09, 2011 2:13 pm

Palette images and transparency

Post by bonzairob »

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:

Code: Select all

$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!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Palette images and transparency

Post by McInfo »

The GIF seems to be created correctly with this line removed.

Code: Select all

imagetruecolortopalette($imtc, false, 32);
I don't see any benefit to calling the function.
Post Reply