PHP GD Graphics Library

by bookmarkscjl on 2008-08-26 11:14:29

PHP is not limited to just producing HTML output. It can also create and manipulate various image file formats, including GIF, PNG, JPG, WBMP, and XPM. Conveniently, PHP can directly stream image data to the browser. To process images, the GD library must be included when compiling PHP for image functions. Depending on which image formats need to be supported, GD and PHP may require additional libraries.

You can use image functions in PHP to get the size of JPEG, GIF, PNG, SWF, TIFF, and JPEG2000 images.

**Note:** Refer to the requirements section to learn how to extend image capabilities for reading, writing, and modifying images as well as reading metadata from photos taken by digital cameras.

If you have the GD library, you can create and manipulate images. The image formats that can be handled depend on the version of GD installed, along with other libraries GD might use to access these formats. GD versions below 1.6 support the GIF format but do not support PNG; versions above 1.6 support PNG but do not support GIF.

**Note:** Starting with PHP 4.3, a version of the GD library is bundled. This bundled version includes some additional features like alpha blending and should be preferred because it is better maintained and more stable.

You may want to enhance GD’s ability to handle different image formats.

**Table 1. Supported Image Formats**

| Image Format | Library to Download | Notes |

|-------------|---------------------|-------|

| GIF | - | Supported only in GD versions below 1.6. Read-only support for GIF was added in PHP 4.3.0 and the bundled GD library. |

| JPEG-6b | ftp://ftp.uu.net/graphics/jpeg/ | - |

| PNG | http://www.libpng.org/pub/png/libpng.html | Supported only in GD versions above 1.6. |

| XPM | ftp://metalab.unc.edu/pub/Linux/libs/X/!INDEX.html | Likely already available if your system has an X environment installed. |

You may want to enhance GD's ability to handle different fonts. The following font libraries are supported:

**Table 2. Supported Font Libraries**

| Font Library | Download Location | Notes |

|--------------|-------------------|-------|

| FreeType 1.x | http://www.freetype.org/ | - |

| FreeType 2 | http://www.freetype.org/ | - |

| T1lib | ftp://sunsite.unc.edu/pub/Linux/libs/graphics/) | Supports Type 1 fonts. |

If PHP is compiled with --enable-exif, it can manipulate information stored in the headers of JPEG and TIFF image files. This allows reading metadata from photos taken by digital cameras. These functions do not require the GD library.

**Note:** PHP does not require any additional libraries to support the exif module.

To activate GD support, configure PHP with --with-gd[=DIR], where DIR is the base installation directory of GD. To use the recommended bundled version of GD (first bundled in PHP 4.3.0), use --with-gd. On Windows, add the GD2 DLL php_gd2.dll to the extensions in php.ini. The GD1 DLL php_gd.dll was removed starting with PHP 4.3.2. Note that preferred true color image functions such as imagecreatetruecolor() require GD2.

To disable GD support in PHP 3, configure with --without-gd.

To enhance GD's ability to handle more image formats, specify --with-XXXX configuration switches when configuring PHP.

**Table 3. Supported Image Formats**

| Image Format | Configuration Switch |

|--------------|----------------------|

| JPEG-6b | Add --with-jpeg-dir=DIR to enable jpeg-6b support. |

| PNG | Add --with-png-dir=DIR to enable PNG support. Note that libpng requires the zlib library, so add --with-zlib-dir[=DIR]. |

| XPM | Add --with-xpm-dir=DIR to enable XPM support. If the required libraries cannot be found during configuration, try adding the path to the X11 libraries. |

To enhance GD's ability to handle more fonts, specify --with-XXXX configuration switches when configuring PHP.

**Table 4. Supported Font Libraries**

| Font Library | Configuration Switch |

|--------------|----------------------|

| FreeType 1.x | Add --with-ttf[=DIR] to enable FreeType 1.x support. |

| FreeType 2 | Add --with-freetype-dir=DIR to enable FreeType 2 support. |

| T1lib | Add --with-t1lib[=DIR] to enable T1lib (Type 1 fonts) support. |

| Native TrueType String Functions | Add --enable-gd-native-ttf to enable native TrueType string function support. |

The behavior of these functions is affected by the global configuration file php.ini.

When the mbstring module is available, exif supports automatic conversion of Unicode and JIS character encodings in user comments. This is achieved by decoding the comment using the specified character set and then encoding the result with another character set suitable for HTTP output.

**Table 5. Exif Configuration Options**

| Name | Default Value | Changeable Scope |

|---------------------|--------------------|------------------|

| exif.encode_unicode | "ISO-8859-15" | PHP_INI_ALL |

| exif.decode_unicode_motorola | "UCS-2BE" | PHP_INI_ALL |

| exif.decode_unicode_intel | "UCS-2LE" | PHP_INI_ALL |

| exif.encode_jis | "" | PHP_INI_ALL |

| exif.decode_jis_motorola | "JIS" | PHP_INI_ALL |

| exif.decode_jis_intel | "JIS" | PHP_INI_ALL |

For more details and definitions of the PHP_INI_* constants, see ini_set().

Here is a brief explanation of the configuration options:

**exif.encode_unicode string**

Defines the character set used to process UNICODE user comments. The default is ISO-8859-15, which works for most non-Asian countries. This setting can be empty or must be one of the encodings supported by mbstring. If empty, the current internal encoding used by mbstring will be used.

**exif.decode_unicode_motorola string**

Defines the internal character set of the image for Unicode-encoded user comments if the image is Motorola byte order (big-endian). This setting cannot be empty but can specify a list of encodings supported by mbstring. The default is UCS-2BE.

**exif.decode_unicode_intel string**

Defines the internal character set of the image for Unicode-encoded user comments if the image is Intel byte order (little-endian). This setting cannot be empty but can specify a list of encodings supported by mbstring. The default is UCS-2LE.

**exif.encode_jis string**

Defines the character set used to process JIS user comments. The default is an empty value, forcing the function to use the current internal encoding used by mbstring.

**exif.decode_jis_motorola string**

Defines the internal character set of the image for JIS-encoded user comments if the image is Motorola byte order (big-endian). This setting cannot be empty but can specify a list of encodings supported by mbstring. The default is JIS.

**exif.decode_jis_intel string**

Defines the internal character set of the image for JIS-encoded user comments if the image is Intel byte order (little-endian). This setting cannot be empty but can specify a list of encodings supported by mbstring. The default is JIS.

This extension does not define any resource types.

These constants are defined by this extension and are only valid if the extension is compiled into PHP or dynamically loaded at runtime.

**Examples:**

Example 1. Creating a PNG image with PHP

```php

```

This example should be called on a page with a tag similar to: ``. The `button.php` script retrieves the "text" string, overlays it on the original image (in this case, "images/button1.png"), and outputs the resulting image. This method allows easy modification of button text without having to redraw the button image each time, enabling dynamic generation.

**Directory:**

- gd_info -- Get information about the currently installed GD library.

- getimagesize -- Get image size.

- image_type_to_extension -- Get file extension for image type.

- image_type_to_mime_type -- Get MIME type for image type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype.

- image2wbmp -- Output image to browser/file as WBMP.

- imagealphablending -- Set the blending mode for an image.

- imageantialias -- Whether to use antialiasing.

- imagearc -- Draw an elliptical arc.

- imagechar -- Draw a horizontal character.

- imagecharup -- Draw a vertical character.

- imagecolorallocate -- Allocate a color for an image.

- imagecolorallocatealpha -- Allocate a color + alpha for an image.

- imagecolorat -- Get the color index of a pixel.

- imagecolorclosest -- Get the closest color index to the specified color.

- imagecolorclosestalpha -- Get the closest color + alpha to the specified color.

- imagecolorclosesthwb -- Get the closest hue/whiteness/blackness to the given color.

- imagecolordeallocate -- Deallocate a color for an image.

- imagecolorexact -- Get the exact color index.

- imagecolorexactalpha -- Get the exact color + alpha index.

- imagecolormatch -- Match palette colors to true color.

- imagecolorresolve -- Resolve the specified color index or nearest alternative.

- imagecolorresolvealpha -- Resolve the specified color + alpha index or nearest alternative.

- imagecolorset -- Set the color for a specified palette index.

- imagecolorsforindex -- Get the color for a specified index.

- imagecolorstotal -- Get the total number of colors in the image palette.

- imagecolortransparent -- Define a color as transparent.

- imagecopy -- Copy part of an image.

- imagecopymerge -- Merge part of an image.

- imagecopymergegray -- Merge part of an image in grayscale.

- imagecopyresampled -- Resample and resize part of an image.

- imagecopyresized -- Resize part of an image.

- imagecreate -- Create a new palette-based image.

- imagecreatefromgd2 -- Create a new image from a GD2 file or URL.

- imagecreatefromgd2part -- Create a new image from a portion of a GD2 file or URL.

- imagecreatefromgd -- Create a new image from a GD file or URL.

- imagecreatefromgif -- Create a new image from a GIF file or URL.

- imagecreatefromjpeg -- Create a new image from a JPEG file or URL.

- imagecreatefrompng -- Create a new image from a PNG file or URL.

- imagecreatefromstring -- Create a new image from an image stream in a string.

- imagecreatefromwbmp -- Create a new image from a WBMP file or URL.

- imagecreatefromxbm -- Create a new image from an XBM file or URL.

- imagecreatefromxpm -- Create a new image from an XPM file or URL.

- imagecreatetruecolor -- Create a new true color image.

- imagedashedline -- Draw a dashed line.

- imagedestroy -- Destroy an image.

- imageellipse -- Draw an ellipse.

- imagefill -- Fill an area.

- imagefilledarc -- Draw and fill an elliptical arc.

- imagefilledellipse -- Draw and fill an ellipse.

- imagefilledpolygon -- Draw and fill a polygon.

- imagefilledrectangle -- Draw and fill a rectangle.

- imagefilltoborder -- Fill an area up to a border.

- imagefilter -- Apply a filter to an image.

- imagefontheight -- Get font height.

- imagefontwidth -- Get font width.

- imageftbbox -- Get bounding box for text using FreeType 2 fonts.

- imagefttext -- Write text to an image using FreeType 2 fonts.

- imagegammacorrect -- Apply gamma correction to a GD image.

- imagegd2 -- Output GD2 image.

- imagegd -- Output GD image to browser/file.

- imagegif -- Output image to browser/file in GIF format.

- imageinterlace -- Activate or deactivate interlacing.

- imageistruecolor -- Check if an image is true color.

- imagejpeg -- Output image to browser/file in JPEG format.

- imagelayereffect -- Set the alpha blending flag to use bundled libgd layering effects.

- imageline -- Draw a straight line.

- imageloadfont -- Load a new font.

- imagepalettecopy -- Copy a palette from one image to another.

- imagepng -- Output image to browser/file in PNG format.

- imagepolygon -- Draw a polygon.

- imagepsbbox -- Get bounding box for text using PostScript Type1 fonts.

- imagepscopyfont -- Copy a loaded font for modification.

- imagepsencodefont -- Change the character encoding vector in a font.

- imagepsextendfont -- Extend or compress a font.

- imagepsfreefont -- Free memory allocated for a PostScript Type1 font.

- imagepsloadfont -- Load a PostScript Type1 font from a file.

- imagepsslantfont -- Slant a font.

- imagepstext -- Draw a text string on an image using PostScript Type1 fonts.

- imagerectangle -- Draw a rectangle.

- imagerotate -- Rotate an image by a given angle.

- imagesavealpha -- Preserve full alpha channel information (instead of single-color transparency) when saving PNG images.

- imagesetbrush -- Set the brush image for drawing lines.

- imagesetpixel -- Draw a single pixel.

- imagesetstyle -- Set the style for drawing lines.

- imagesetthickness -- Set the thickness for drawing lines.

- imagesettile -- Set the tile image for filling areas.

- imagestring -- Draw a horizontal string.

- imagestringup -- Draw a vertical string.

- imagesx -- Get image width.

- imagesy -- Get image height.

- imagetruecolortopalette -- Convert a true color image to a palette image.

- imagettfbbox -- Get bounding box for text using TrueType fonts.

- imagettftext -- Write text to an image using TrueType fonts.

- imagetypes -- Return the image types supported by the current PHP version.

- imagewbmp -- Output image to browser/file in WBMP format.

- imagexbm -- Output XBM image to browser/file.

- iptcembed -- Embed binary IPTC data into a JPEG image.

- iptcparse -- Parse binary IPTC data into individual tags.

- jpeg2wbmp -- Convert a JPEG image file to a WBMP image file.

- png2wbmp -- Convert a PNG image file to a WBMP image file.