| LAST | The Basics | NEXT |
| Let's Get to Some PHP Already! |
Image creation in PHP is handled in 5 basic steps:
|
| 1. Create the Image: |
| resource imageCreate ( int x_size, int y_size) resource imageCreateTrueColor ( int x_size, int y_size) Returns an image identifier representing a blank image of size x_size by y_size. |
| Image creation is done with one of two functions imageCreate() and imageCreateTrueColor(). The latter creates a true color image and the former creates an index color image. Both functions return an integer image identifier and require 2 parameters representing the width and height of the image in pixels. The PHP online manual recommends the use of imageCreateTrueColor(), but if you understand its limitations, imageCreate() can often be a more appropriate choice. |
| 2. Add content: |
| Once you have a blank image in memory, there are many copy, transform, draw, and text/font functions to create your masterpiece. These will be covered later in the presentation. |
| 3. Send the Headers: |
| int header ( string string [, bool replace [, int http_response_code]]) Used to send raw HTTP headers. See the HTTP/1.1 specification for more information. |
| Since you're going to be sending image data to the browser instead of HTML, you need to use the header function to send the appropriate content type: image/png, image/jpeg, etc. |
| 4. Output the Image |
| int imagePNG ( resource image [, string filename]) Outputs a GD image stream ( image ) in PNG format to standard output (usually the browser) or, if a filename is given by the filename it outputs the image to the file. int imageJPEG ( resource image [, string filename [, int quality]]) int imageWBMP ( resource image [, string filename [, int foreground]]) |
| In PHP you manipulate images within memory in GD's native format. The functions listed above are for outputting your image in whatever format you need to use. Like true color vs. indexed, what format you choose will depend upon the particular situation. In general JPEGs are good for true color, photographic images and PNGs are good for indexed color graphics. WBMP, the Wireless Bitmap format, is used for two-color images for WML pages. GIF is similar to PNG, but inferior in every way and has patent trouble, so is best avoided. The GD & GD2 formats are, according to the GD FAQ page "not intended for general purpose use and should never be used to distribute images. It is not a compressed format. Its purpose is solely to allow very fast loading of images your program needs often in order to build other images for output." |
| 5. Delete the Image |
| int imageDestroy ( resource image) Frees any memory associated with image image. image is the image identifier returned by the imageCreate() function. |
| Whether you output the image to a browser, save it to a file, or just give up it will remain in the server's memory until it is destroyed. If you continue to build images without destroying them, the server will crash. Never create an image without destroying it by the end of your script. |
| LAST | Table of Contents | Function Index | NEXT |