Chapter 17. Graphics

Introduction

With the assistance of the GD library, you can use PHP to create applications that use dynamic images to display stock quotes, reveal poll results, monitor system performance, and even create games. However, it’s not like using Photoshop or GIMP; you can’t draw a line by moving your mouse. Instead, you need to precisely specify a shape’s type, size, and position.

GD has an existing API, and PHP tries to follows its syntax and function-naming conventions. So if you’re familiar with GD from other languages, such as C or Perl, you can easily use GD with PHP. If GD is new to you, it may take a few minutes to figure it out, but soon you’ll be drawing like Picasso.

The feature set of GD varies greatly depending on which version of GD you’re running and which features were enabled during configuration. GD can support GIFs, JPEGs, PNGs, and WBMPs. GD reads in PNGs and JPEGs with almost no loss in quality. Also, GD supports PNG alpha channels, which allow you to specify a transparency level for each pixel.

Besides supporting multiple file formats, GD lets you draw pixels, lines, rectangles, polygons, arcs, ellipses, and circles in any color you want. Drawing Lines, Rectangles, and Polygons covers straight shapes, and Drawing Arcs, Ellipses, and Circles covers the curved ones. To fill shapes with a pattern instead of a solid color, see Drawing with Patterned Lines.

You can also draw text using a variety of font types, including built-in and TrueType fonts. Drawing ...

Get PHP Cookbook, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.