We have a new update on The Future of the Penny Arcade Forums.

PHP, GD, and transparent PNGs (24 bit) + Fonts

SeguerSeguer of the VoidSydney, AustraliaRegistered User regular
edited January 2011 in Help / Advice Forum
Hey guys,

Has anyone had experience using PHP and GD to:

1. Create transparent PNGs (I got this part working)
2. Add transparent PNGs on top of another one (this part works except for transparency - I get white instead)

Currently what I have is:

- Create transparent image canvas
- Add png image with transparency

This image resource comes out fine using imagepng(), but if I try to add another transparent PNG to this image resource they come out with the white background.

I've used every combination I could find in the comments on php.net, for most people it just seems to work.

Code below:
$image = imagecreatetruecolor($width, $height);
//turn on alpha transparency
imagealphablending($image, false);
imagesavealpha($image, true);

$base = imagecreatefrompng($base);
imagecopyresampled($image, $base, 0, 0, 0, 0, $width, $height, $width, $height);

return $image;

(creates the canvas, adds png)

If I do this with my other PNG and output it, it's transparent, but as soon as I merge them (imagecopy() or imagecopyresample()) I get the white background.

I've also tried to write text, however PHP/GD continue to complain that it can't find the .ttf files, however I reference them, and no matter where they are. I'm on Windows, and I tried putting the ttf in the same directory as php file, relative paths, absolute paths, etc..

Seguer on

Posts

  • clam2000clam2000 SeattleRegistered User regular
    edited January 2011
    Try applying imgsaveapha / imagealphablending false to $base as well.

    You should also see if works with imagecopy or imagecopyresized. (the re-sampling might not be doing the right thing with the alpha channel.)

    clam2000 on
    cdogwal.gif
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited January 2011
    That code there works, the code with $base adds the PNG with transparency to the canvas.

    If I use that code, with my other image as $base, it works.

    When I use imagecopy or imagecopyresampled, the source loses its transparency when merged into the destination

    Seguer on
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited January 2011
    Alright I fixed the image problem, you have to use

    imagealphablending($image, true);

    Where $image is the "bottom" image (the one you are merging the new image ON to)

    However my font problem remains

    Seguer on
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited January 2011
    Alright I've fixed my problems. Thanks for anyone who read the thread!

    Seguer on
Sign In or Register to comment.