Image creation and manipulation using the gd Graphics Library
April 1, 2006
The gd Graphics Library is an open source code library for the dynamic creation and manipulation of images, usually for the dynamic production of charts, graphics, thumbnails, etc. by web sites. This happens server-side, i.e. on the web server, not on the viewer's system - the code is invisible to end users.
GD is written in C; versions are available for Perl, PHP and other languages. PNG, JPEG and GIF images, including animated GIFs, can be produced. FreeType and TrueType text can be added, complex graphics drawn, most image conversions are handled fairly well.
GD comes standard with PHP 4.3.x and beyond. If your web hosts offers PHP scripting, you probably already have access to these image functions. GD is not as powerful as ImageMagick (not even close), or as easy to use - but it easily exceeds most users' requirements. It does not require the PHP exec() function to run - a huge safety plus (though with MagickWand for PHP you can also avoid this problem, if your web hosting company is willing to install it for you).
Check www.php.net for all the details; the PHP manual doesn't reference gd functions in the index - but you'll find them all under 'Image Functions.'
If you run your own web server with PHP (version 4.3 or better), open php.ini and ensure that extension_dir points to the directory in which PHP is installed, i.e.
extension_dir = "c:/php/extensions"
and enable gd by uncommenting the "#extension=php_gd2.dll" line in php.ini, i.e.
extension = php_gd2.dll
(You'll find both lines in the php.ini file, though in different locations.)
Because gd functions aren't the easiest to use, I present the ones I use as a series of articles here - download the code and use it straight, if you wish, or adapt it to your own needs.
Jeff Knight of New York PHP provides the best tutorial I've found on the subject - thorough, complete, and very easy to follow. Recommended.
More on the gd graphics library
Checking for and installing the gd Graphics Library
Custom grayscale function for dynamic image conversion using PHP and the gd Graphics Library
See also our article on Image creation, conversion and manipulation with ImageMagick.