Introduction to GD

 
Published on 2004-07-30 by John Collins. Socials: YouTube - X - Spotify - Amazon Music - Apple Podcast

What is GD?

GD is a graphics library that is bundled with the PHP web scripting language, as of version 4.3.0. GD is an open source project, under the stewardship of Thomas Boutell, whose web site http://www.boutell.com/gd/ contains downloads and documentation on the latest version (2.0.27 as of writing).

GD allows PHP programmers to developer web applications that facilitate generating dynamic images, in the JPEG and PNG formats. These images may be based on user input, database queries or other variable information, which allows for a very powerful and flexible tool for serving up relevant information to end users in a graphical format, e.g. in bar or pie charts.

The GD library is written in C, and is available for Windows and Unix-derivative systems. Although commonly used with PHP, implementations are also available for other programming languages, for example GD.pm for Perl.

An overview of GDs functionality

The functions provided by GD vary greatly depending upon which major version of GD that you have installed on your system. Versions before 1.6 only supported the GIF format. Version 1.6 and later support JPEG, PNG and WBMP, but not GIF, due to a patent on that graphic format .Version 2.x added several new drawing shapes, and it is that version that I will be using in future tutorials on GD.

To give some understanding of GDs power, here are some of its major features:

Apart from the features mentioned above, there are likely to be many more additions to come from this very active project. While there are other tools available for generating images dynamically with PHP, GD is the most widely used and provides the best performance.

Making use of GD

In order to use GD 2, you need to have a web server with PHP installed, along with GD support enabled in your PHP setup. You may also need to have the GD library installed separately, although this is usually bundled with the PHP download for most operating systems (although not the Windows installer!).

As for possible uses for GD, here are a few suggestions:

As you can see, GD can have many uses. While static images are fine for most purposes and are widely used throughout the Internet, dynamic images provide an extra layer of functionality to a PHP system that allow it to make that jump from being a web site towards being a web application.

In my next piece on GD, I will show you how to draw basic shapes in GD, before moving onto working with text, and then finally some real-life examples of the libraries functions. Until then, I would suggest some further research on installing GD at http://www.boutell.com/gd/ and PHP.net, which is beyond the scope of this introductory article.


Updated 2020 : note that the above post is out-of-date, given this post was originally published in 2004, but is left here for archival purposes. Unfortunately some of the URLs above are now dead, so I have de-linked them.