Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- GD: making a png background transparent
January 16, 2009, 5:21 pm
rate this thread
1 out of 5
1 out of 5
images with transparent backgrounds (created in Photoshop) and
paste them onto a transparent background (created by GD).
Unfortunately, the transparent part isn't working and I don't
know why not.
Here's the code, with the loop stripped out. The loop just
thumbs through an integer bitset and chooses images from an array
of array(filename, w, h) accordingly.
$dest = imagecreatetruecolor( $destw, $desth ) ;
list($fn, $srcw, $srch) = $image[$i] ;
$destx = (int)(($destw - $srcw)/2) ;
$xparent = imagecolorresolvealpha( $dest, 255,2,240,1 ) ;
$src =imagecreatefrompng($fn) ;
imagecopymerge( $dest, $src,
$srcw, $srch, 100 ) ;
...and then I call header() and imagepng() it out.
The background on the src png I load was transparent when I saved
it off from Pshop, and it's transparent if I pass it to <img>.
But the product of imagecreatefrompng() has a black background.
So instead of getting the non-rectangular image floating in a
transparent background, it might as well be a jpg file.
I'm sure I'm misunderstanding something dopey, but I'm cursed if
I can figure out what.
Any useful insights greatly appreciated.
Re: GD: making a png background transparent
Oh, and I ran into that exact problem at my last job. It's been a
couple of months, though, so I'm not 100% certain of the fix.. but I
seem to recall that I had to use imagecopy() instead of imagecopymerge
(). I could be wrong, but I distinctly recall having to do some that
seemed really counterintuitive.
Hope that helps!