Forum Moderators: coopster

Message Too Old, No Replies

GD Transparency Problem

...arrgh!

         

MJMason

2:40 pm on Apr 2, 2008 (gmt 0)

10+ Year Member



I'm using GD to overlay a selection of images with transparent backgrounds, and I can get this working fine, however when I then try to overlay the generated image on top of a semi transparent white image, the background turns white instead of remaining semi transparent.

I have also tried dynamically generating the background but this will only go to fully transparent or fully opaque for some reason, despite changing the alpha setting.

The page and code is below, it's probably something stupid but I've tried so many combinations of settings that I've almost given up, so some help would be much appreciated :]

<snipped url>

PHP Code:


<?php
/* Attempt to overlay transparent and semi-transparent images */
$width = 120;
$height = 120;
$bottom_image = imagecreatefrompng("bottom_image.png");
$top_image = imagecreatefrompng("top_image.png");
imagesavealpha($top_image, true);
imagealphablending($top_image, false);
imagecopyresampled($bottom_image, $top_image, 0, 0, 0, 0, $width, $height, $width, $height);
imagepng($bottom_image, "new_image.png");
?>
Image generated at <a href="new_image.png">new_image.png</a>, see on a coloured bg <a href="test.html">here</a>

Many thanks,
Michael

[edited by: coopster at 3:06 pm (utc) on April 2, 2008]
[edit reason] no urls please TOS [webmasterworld.com] [/edit]

whoisgregg

4:16 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, MJMason!

Instead of using imagecopyresampled, use imagecopy or imagecopymerge (with a $pct of 100).

Also, it's often tricky to manage the correct settings for imagesavealpha and imagealphablending. You probably need to set those both to be true on both the $bottom_image and $top_image handlers.

However, I have a lot of experience with dynamic image generation and I still sometimes have to play around with different combinations of true/false for those two settings before I get it right... Which is why GD often stands for something else when I am debugging similar problems. ;)

MJMason

4:56 pm on Apr 2, 2008 (gmt 0)

10+ Year Member



I've tried every imagecopy* command, and every settings for imagesavealpha and imagealphablending, and in a script which is combining around 20 images and adding around 15 text labels as well...

For now I've just used command line imagemagick with exec("convert bottom_image.png top_image.png -composite new_image.png"); I've spent far too many hours trying to find the combination of settings with GD, and for this task IM is much simpler :]

Thanks for the help though :]

Mike

[edited by: MJMason at 4:56 pm (utc) on April 2, 2008]

whoisgregg

7:48 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



every imagecopy* command, and every settings for imagesavealpha and imagealphablending

Yup, been there many many times. :) Glad you got it sorted, Imagemagick is an excellent solution. I only really worry about getting GD working when IM is too slow for the task at hand.