5.4. Dynamic images on the command line

It is also possible to generate images directly using the command line version of PHP. This works the same way as the normal "through the browser" generation with the exception that no HTTP headers will be generated. Only the binary image data.

This could be an effective way to make an efficient web-site by having some automatic creation of images at regular interval (perhaps using some kind of cron-job) that are referenced as usual (with an <img> tag) in the scripts. This will avoid having to re-generate the image every time a visitor hits the site.

Please make sure that you run the command line version of PHP (cli). Using the CGI SAPI version of PHP will not work since then the HTTP headers will be generated.

Note

If the CGI version is used the generation of headers may be suppressed by adding the '-q' option.

You can easily check the version installed by the command line

$/> php --version

this should give a reply with something like

PHP 4.3.8 (cli) (built: Aug 29 2004 18:48:13) Copyright (c) 1997-2004 The PHP Group Zend Engine v1.3.0, 
Copyright (c) 1998-2004 Zend Technologies

The important thing here is the (cli) marker. The JpGraph library check from what SAPI API it is invoked from and adjusts the header generation accordingly.

If all the above requirements are met then images can be generated directly on the command line and stored in a suitable file. For example by

$/> php myimage.php > image.png

Please note that the file extension on the image file should match the format in which the image is generated.