There are hundreds of libraries available to add on to the PHP base, and GD is just one of those with thousands of possibilities.  Used to process images and even dynamically create and output graphics, the GD library is relatively easy to learn with distinct potential in the graphics world. In this article, I will establish the basics of image-handling functions to advance later.

Nearly any computer-literate user can click the mouse a few times and create a decent image with software like Photoshop or another graphics program, but there is much potential in the uses of the PHP GD library and its ability to construct images on the fly, like charts and graphs.  Additionally, it can be used in other manners, such as handling uploaded images and dynamically outputting thumbnails and edited images.

For the experimenter-at-heart, there may also be a future in image editing via the web, using PHP that is.  Some websites already handle activities like this, such as SnipShot, but they do not necessarily utilize fully the possibilities PHP provides.  It certainly does leave much to the imagination as to just what a programming language could do with images.

So how exactly is the GD libary used?  First, you should make sure you have it instead of spending forever stressing over error messages about undefined functions.  If you have PHP 4.3 or later, the GD libary was bundled with your version, so there’s no need to worry, though more libraries may be required for more image types and fonts.  Still not sure if you have the GD library?  Check your phpinfo by running the following function in an empty PHP file and opening it in your browser:


Searching for the “gd” section will help you determine if it is enabled or not, and once you have the library ready for use, it’s time to create a simple image.  However, I suggest you first take some time to familiarize yourself with the following terms/ideas:

The main thing to realize with outputting an image is that the content type must be declared before the image can truley be output to the page, whether directly in the page accessed or through inclusion of a separate PHP “image” file.  Below is an extremely basic image, as created with PHP, that we will analyze line-by-line to explain the main functions necessary to begin an image.

header("Content-type: image/png");
$image = imagecreatetruecolor(300,300);
$black = imagecolorallocate($image,0,0,0);

Placing the code above in a PHP file should reveal a 300px-by-300px black square, and here’s how that is achieved:

  • Line 2:  Declare the content type of the file.  The browser will read this as a PNG image.  Naturally, PNG can be replaced with any supported image type (e.g. GIF, JPEG, etc)
  • Line 3:  Create the image, and assign it to the variable ‘image.’  The values read as width, then height, in pixels.
  • Line 4:  Allocate a color to a variable, and in this case, assign that color as the background of the image.  Any and all colors used in the image must be established this way, calling the image variable first, then in RGB format, declaring the R, G, and B values.
  • Line 5:  Output the image to the file.  Depending on the image type, the function will change (e.g. images of type ‘gif’ would use imagegif(), etc)
  • Line 6:  Destroy the memory associated with the image.  In many cases, this is necessary to keep future scripts working correctly, and to free memory and prevent overloading the server with large images.  Though sometimes the image will be destroyed automatically, you are better off doing it manually and being sure the space is free.

With just these few lines, a (very basic) image is created.  Looking at the list of functions associated with the GD library, you can see that there is a myriad of things that can be accomplished with the library, and in the future I will detail some specific uses, but perhaps the best way to discover a new method is to experiment yourself, reading the documentation and trying out your own ideas.

Leave a Reply

Wordpress doesn't like it when you post PHP code. Go save your code at pastebin, and post the link here.

About the Author

Kurtis has been working with PHP for nearly four years, and he has moderate experience with MySQL as well as other programming languages, like Java and C++.