tags:

views:

219

answers:

6
+5  Q: 

Images in PHP

Is it possible to create images with PHP (as opposed to simply linking to them via HTML) and if so, where should I go first to learn about such a thing?

+4  A: 

Yes this is possible. I believe there are multiple libraries to accomplish this. The most widely used is probably ImageMagick which is actually not PHP specific but comes with appropriate bindings.

See also in the PHP documentation.

Konrad Rudolph
+2  A: 

Check out GD. It contains a ton of functions for image creation,manipulation and interrogation. Your PHP install just has to built with the GD library which it probably was.

Twotymz
+1  A: 

PHP GD

Pear Image_Canvas (and Image_Graph for graphs)

Those are the two I know of.

Annan
+10  A: 
Ross
You should always try to use the header() function at the last moment possible (such as, before the imagepng() funciton). The way it is now, if the php script dies "Cannot Initialize new GD image stream" the browser will try to interpret it as a gif, so it won't be intelligible.
stalepretzel
+3  A: 

For decent tutorials on image generation using PHP:

GD - http://devzone.zend.com/node/view/id/1269

ImageMagick - http://www.sitepoint.com/article/dynamic-images-imagemagick

Edmund Tay
+1 yes, good tutorial indeed, thanks!
tharkun
A: 

MagickWand is pretty good for that as well, and pretty powerful.

http://www.bitweaver.org/doc/magickwand/index.html

This snippet will take an image, wrie the 'rose' in Vera, or whatever fonts are available, and flush the image to the browser.

$drawing_wand=NewDrawingWand(); DrawSetFont($drawing_wand,"/usr/share/fonts/bitstream-vera/Vera.ttf"); DrawSetFontSize($drawing_wand,20); DrawSetGravity($drawing_wand,MW_CenterGravity); $pixel_wand=NewPixelWand(); PixelSetColor($pixel_wand,"white"); DrawSetFillColor($drawing_wand,$pixel_wand); if (MagickAnnotateImage($magick_wand,$drawing_wand,0,0,0,"Rose") != 0) { header("Content-type: image/jpeg"); MagickEchoImageBlob( $magick_wand ); } else { echo MagickGetExceptionString($magick_wand); }

Chris Henry