GD: making a png background transparent

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm doing a little graphics stuff where I take existing png
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 ) ;
        imagefilledrectangle($dest,0,0,$destw,$desth,$xparent) ;
        $src =imagecreatefrompng($fn) ;
        imagecopymerge( $dest, $src,
                        $destx, $desty,
                        0, 0,
                        $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


Quoted text here. Click to load it

should have been

    $xparent = imageColorAllocateAlpha($dest,255,2,240,0) ;

I posted too quickly :-}

Re: GD: making a png background transparent

Quoted text here. Click to load it

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!



Site Timeline