Oxxus » Tutorials » PHP turorial » GD library

GD library

GD is an open source code library for the dynamic creation of images.

GD is used for creating PNG, JPEG and GIF images and is commonly used to generate charts, graphics, thumbnails on the fly.

While not restricted to use on the web, the most common applications of GD involve web site development.

The GB library was originally developped by Thomas Boutell and is now maintained by Pierre-A. Joye under the umbrella of PHP.net

Requirements

Using gd requires an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant.

Any full-ANSI-standard C compiler should do. Most Unix/Linux OS based systems has the gcc which is ANSI compliant.

As of version 1.6, the zlib compression library and the libpng library are required.

As of version 1.6.2, text using anti aliased TrueType fonts can be drawn if the libttf library is installed, but this is not mandatory.

Zlib is available for almost every platform from the zlib web site. Libpng is available for a variety of platforms from the PNG web site.

A PNG viewer is also required and is supported through almost every web browser that currently exists.

The GD library can be added to the php installation if the php services are compiled besides the bundle package offered within the apache installation and therefore any additional installation won't be required and will be available within the php itself.

In order to find out if the installed PHP services provides the GD library support the php info page should be created with the code below.

The file phpinfo.php with the contents stated below and access the url for example http://www.test.com/phpinfo.php

<?php
phpinfo();
?>

Using GD libraries within the PHP

<?php header("Content-type: image/png"); $string = $_GET['text']; $im = imagecreatefrompng("images/button1.png"); $orange = imagecolorallocate($im, 220, 210, 60); $px = (imagesx($im) - 7.5 * strlen($string)) / 2; imagestring($im, 3, $px, 9, $string, $orange); imagepng($im); imagedestroy($im); ?>

This example would be called from a page with a tag like:

<imgsrc="button.php?text=text">

The above button.php script then takes this "text" string and overlays it on top of a base image which in this case is "images/button1.png" and outputs the resulting image.

This is a very convenient way to avoid having to draw new button images every time you want to change the text of a button.

With this method they are dynamically generated.

Contact sales!