8.2 GD

GD is an excellent, relatively simple graphics toolkit for creating or modifying graphic images. You may create new images or read in images from several formats to edit or merge. GD contains a number of graphics primitives, objects such as lines and rectangles, which can be added to an image. It also includes support for TrueType fonts.

8.2.1 Installing GD

To install GD.pm on your computer, you should first test to see if the module is already there: try typing perldoc GD to see if the documentation that is installed with the module is running on your computer. If not, GD.pm is available from CPAN, which is the best way to get it (its real home is at http://stein.cshl.org).

The GD documentation explains which supporting libraries are necessary. In particular, you need to have Thomas Boutell's gd C library installed, and in the proper version, to work with the version of GD.pm you are installing. To work with different graphics file formats, the gd library is best compiled in the presence of additional libraries that handle PNG, JPEG, zlib compression, and the FreeType version of TrueType fonts. How to obtain and install these additional packages is all explained in the GD documentation. You may need extra time to get these various pieces into place before you can get GD to do everything it can do.

So, even if you use CPAN to install GD, the installation requires some information about those additional packages; it's a good idea to look at the documentation first, and to get those libraries in place, before installing GD. To install on my Linux system, I become root and issue the following commands:

perl -MCPAN -e shell;
cpan> install GD

8.2.2 Using GD

The following list is a partial overview of GD's capabilities:

  • To create a new image, you call the new constructor with x and y values of the size in pixels of the desired image. You can also choose between a palette or a truecolor image. And you can either create a new image or open an existing image in a few common formats (such as PNG, JPEG, GD, and XPM).

  • You can output images as PNG, JPEG, WBMP (a bitmap image format), or its own GD format and the compressed version GD2.

  • The color table can be manipulated in several ways. A new color can be added or an old one deleted. The closest match in the color table to specified red, green, and blue values can be returned, and the RGB values of an existing color table entry can be determined. One color can be designated as transparent so that portions of an image drawn in that color are invisible.

  • Lines can be drawn using defined "brushes," or in certain defined styles (like dotted lines, for example). Shapes may be filled with repeated copies of another image, or "tiled."

  • You can draw individual pixels, lines, dashed lines, rectangles, polygons, and arcs. You can fill regions of the image (such as the interiors of rectangles) in various ways.

  • You can copy regions of images, changing size if desired; you can also rotate and flip images.

  • Text can be written in built-in or TrueType fonts, and rotated. Various methods return information about the fonts.

  • A Perl module called GD::Graph adds some excellent graph drawing capabilities on top of the GD module.

In sum, GD.pm provides easy-to-program and attractive graphics capabilities for generating dynamic web pages (and for other uses as well). Now that you've had an introduction to the topic of graphics programming, and to the GD.pm module, I'll use GD.pm in a Perl script that generates images dynamically to display restriction maps.