Introduction to GD

An introduction to the PHP GD graphics library.

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 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-derivitive systems. Although commonly used with PHP, implementations are also available for other programming languages, for example 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:

  • The drawing of primitive shapes such as lines, arcs, ellipses, polygons et cetera with variable parameters provided by your PHP code.
  • Support for true colour images with an alpha channel, that allows for transparency in images.
  • Support for merging of two images into one composite.
  • Full text support, with built-in fonts and support for antialised TrueType fonts via the FreeType library.
  • Support for antialised primitives.
  • Optional resampling of resized images.

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:

  • Charts, such as pie charts of survey results, as stored in a database.
  • GIS (Geographical Information System), that is drawing maps based on coordinates stored in a spatial database such as MySQL or Oracle.
  • Organizational Structures, for example building a corporate organization chart based on an employee database.
  • Node Trees, such as Binary Trees or any other collection of data that contains parent/child relationships that need to be represented graphically.

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 and, which is beyond the scope of this introductory article.

John Collins

I have been writing about web technology and software development since 2001. I am the developer of the Alpha Framework for PHP, and the personal productivity app. I love open source, technology, and economics.

Basic Shapes in PHP GD

Working With PNG