PNG image transparency in PHP

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
User avatar
ario
Forum Newbie
Posts: 13
Joined: Wed Apr 26, 2006 3:56 pm

PNG image transparency in PHP

Post by ario »

I need to dynamically generate some PNG images for my site. They're pretty simple--a transparent background with some lines, such as this image:

Image

I have a Java program that does this, but my hosting company recently upgraded their PHP version and now any java call (even a HelloWorld program) through the php system function returns an "unable to reserve enough memory for the VM" error. My attempts at a workaround in order to call the Java program some other way have been unsuccessful (even though I have no problem calling it through SSH--they don't have the ssh module installed for php, or I'd try it that way.)

So, I need to figure out how to write this in PHP. I didn't do it in PHP in the first place because my readings through the image functions led me to believe that I couldn't get the desired effect. All I need to do is this:

Draw antialiased lines on a transparent image, and have the antialiasing affect the alpha layer.

Can someone please provide me with some sample code or direction on how to do this? I've searched extensively and haven't been able to find much written on how to do PNGs in PHP.

Thanks in advance.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Take a look at either the GD or ImageMagick extensions to PHP. One should be installed with PHP on your host. You can use one of those libraries to create the images.
(#10850)
User avatar
ario
Forum Newbie
Posts: 13
Joined: Wed Apr 26, 2006 3:56 pm

Post by ario »

Right, I have GD installed and I'm familiar with what it is; what I'm asking is how to use it to do what I specified. (I've tried to figure it out on my own from the libraries and like I said, I've gotten the impression it's not possible, but I'm not really sure about that or why it would be.)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Just a note:

If I remember correctly, IE doesn't support PNG with transparency...so you need to use GIF. Unless your users will use IE7

I did a quick search and came up with this hack:

http://digg.com/programming/Transparent ... ages_in_IE

Cheers :)
User avatar
ario
Forum Newbie
Posts: 13
Joined: Wed Apr 26, 2006 3:56 pm

Post by ario »

I've already got a workaround for that. IE does support PNGs, it's just a hassle.

My problem is in actually *generating* them. I need to figure out how to do this in PHP, unless someone knows how I can hack my hosting company and get them to increase my memory limits.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

ario wrote:My problem is in actually *generating* them.
http://www.phpgd.com/scripts.php?script=27
User avatar
ario
Forum Newbie
Posts: 13
Joined: Wed Apr 26, 2006 3:56 pm

Post by ario »

Thank you. I think that'll be adequate for my purposes for now, and certainly I needed to get that code.

However, I think what I was really asking was if there is a way to have antialiasing affect the alpha component, like Java's image functionality does. The [url =http://www.php.net/manual/en/function.i ... ialias.php]imageantialias function[/url] reads "It does not support alpha components. It works using a direct blend operation. It works only with truecolor images." And I can't seem to find a way to set an individual pixel's alpha component, or else I'd attempt to write an antialias line drawing function myself.

This seems like a pretty straightforward thing to offer, doesn't it? Does anyone know if this is possible, or if not, why not? Like I said, if they provided a way to set an individual pixel's alpha component, this could be done, because the antialias algorithm for a line is pretty straightforward.

Anyone have any ideas?

Thanks again for the code, though, btw.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

imagecolorallocatealpha() with imagesetpixel() will give you a way to draw anti-aliased lines .. slow though ..
User avatar
ario
Forum Newbie
Posts: 13
Joined: Wed Apr 26, 2006 3:56 pm

Post by ario »

Cool, thanks.
Post Reply