Page 1 of 1

PNG image transparency in PHP

Posted: Wed Apr 26, 2006 4:09 pm
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.

Posted: Wed Apr 26, 2006 5:59 pm
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.

Posted: Wed Apr 26, 2006 7:33 pm
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.)

Posted: Wed Apr 26, 2006 7:53 pm
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 :)

Posted: Wed Apr 26, 2006 7:54 pm
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.

Posted: Thu Apr 27, 2006 3:07 am
by onion2k
ario wrote:My problem is in actually *generating* them.
http://www.phpgd.com/scripts.php?script=27

Posted: Thu Apr 27, 2006 12:54 pm
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.

Posted: Thu Apr 27, 2006 4:43 pm
by onion2k
imagecolorallocatealpha() with imagesetpixel() will give you a way to draw anti-aliased lines .. slow though ..

Posted: Fri Apr 28, 2006 9:47 am
by ario
Cool, thanks.