Outputting image using php GD library via function call

Go To StackoverFlow.com


I know that I can output an image using GD by using

 <img src='draw.php'>

Where draw.php is a file containing the code to create an image. How can I instead output the image via a function call (I am using the Zend Framework so will be using a View Helper) rather than simply pointing to a .php file in an img tag as above?

Any help greatly appreciated.

2009-06-16 08:42
by William


you can't.

at least not in a useable way - you could encode the image with base64:

<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." alt=""/>

i don't have any idea which browsers support this, though ... quick test:

  • firefox: ok
  • chrome: ok
  • opera: ok
  • ie6: fail
  • ie7: fail
  • safari: fail

ok, forget it.

but, you're probably trying to do something different - passing the file through ZF. i can't help you with that, but it should work roughly like this:

in your controller, set the output type to image/png (however ZF handles that) pass through your image and make sure ZF doesn't add anything to the output (like additional html and stuff).

2009-06-16 09:02
by stefs


Why not make your View Helper create an image, write it to disk, and then output/return the img tag with the correct source attribute?

2009-06-16 09:25
by David Snabel-Caunt
maybe it's an image that's only displayed once - stefs 2009-06-16 21:26
We will probably never kno - David Snabel-Caunt 2009-06-16 21:59


Send appropriate headers (content type) and then use http://www.php.net/image_jpeg

2009-06-17 16:55
by Tomáš Fejfar