Web Hosting Talk







View Full Version : PHP CG: Image Transparency Problem


Axiss
09-04-2005, 12:12 PM
Ok, here's my problem. I have a .tga file, which I manually converted to a .png in PSP. I maintained the alpha channel transparency of the .tga into the .png file as its transparency. Then I had a file the CG functions could process to extract the 40x40 icons out of this composite file.

Problem is the extracted Icons do not show as transparent in IE, but they work just fine in both Firefox and Opera. So, I figure ok let's try this with GIF format. Still, no dice in IE and there's no transparency in Firefox or Opera now. I must have the transparency in IE. So, below is the code I'm using. Does anyone know what the problem is? I've looked over google and thru php.net and not been able to turn up a solution. Any help is appreciated.

Note: for GIF I just changed the places it says PNG to GIF. The Transparency color settings were added in as part of it too.

$image = imageCreateFromPNG("Icons.png");
$nImage = 1;

for($nI=0; $nI <=5; $nI++)
{
for($nY=0;$nY <=5; $nY++)
{
$image_p = imageCreateTrueColor(40, 40);
$trans = imageColorResolve($image_p, 0, 0, 0);
imageColorTransparent($image_p, $trans);
imageCopyResampled($image_p, $image, 0, 0 , 40 * $nY, 40 * $nI, 40, 40, 40, 40);
imagePNG($image_p, "icon" . $nImage . ".png");
$nImage++;
}
}

laserlight
09-04-2005, 12:18 PM
I've heard that MSIE has a problem with rendering PNG images with alpha transparency - have you tested with PNG images that are known to work?

Axiss
09-04-2005, 12:21 PM
Sorry, I got a bit hasty with the first post. I neglected to mention I tried it with GIF format images as well. I added that info to my first post. And yes I started with a GIF image.

laserlight
09-04-2005, 12:23 PM
The thing is, the MSIE problem apparently only applies to PNG, not to GIF, though there is supposed to be a workaround, but I'm not sure if the workaround is clientside or serverside.

Axiss
09-04-2005, 12:37 PM
Yeah, there's a problem with PNG image alpha channel transparency. The workaround is some sort of filter hack that mostly works, but I didn't get past that. I would prefer a simpler solution if I can find one.

I really should have posted the code as a GIF problem, since that's the way I started out doing things. When the transparency wasn't working there, i switched to PNG and got it working on Firefox. I got myself in a hurry and posted the current code instead of how I got to this point. Sorry for the confusion.

Anyway, my code doesn't work for GIF images either. I'd be happy with GIF if they come out looking ok. I really just need to maintain the image transparency and colors as close as possible in the conversion. Once I get this part working, it's finding a way to automate the .tga conversion since I need to run this kind of routine on dozens of compositie images.

Axiss
09-05-2005, 04:52 AM
After a lot of trial and error, digging thru php.net I fixed the problem. The code that works is below. It also works for saving as GIF images now. Both formats are displaying great for me in IE and firefox now. Took long enough to figure out

$image = imageCreateFromPNG("pieces.png");

for($nI=0; $nI <=5; $nI++)
{
for($nY=0;$nY <=5; $nY++)
{
$image_p = imageCreate(40, 40);
$trans = imageColorAllocate($image_p, 255, 255, 255);
imageColorTransparent($image_p, $trans);

imageCopyResampled($image_p, $image, 0, 0 , 40 * $nY, 40 * $nI, 40, 40, 40, 40);
magePNG($image_p, "Icons/" . $nImage . ".png");

$nImage++;
}
}

Now to find a way to use a .tga file as the original image.