Chapter 8: Advanced PHP Techniques: Working with Images

You can use the built-in image-related functions of PHP to do marvelous and myriad things with images, whether it's creating new images on-the-fly or manipulating static images you already have on your system. The many, many image-related functions are well documented in the PHP Manual, at

In this chapter, you'll learn the basics of creating and manipulating images using PHP functions.

Installation and Configuration Requirements

Beginning with version 4.3.0, PHP includes a bundled version of Thomas Boutell's GD graphics library. This eliminates one step in the installation and configuration process, but if you're using an earlier version of PHP, you will have to do a little more work before tackling the scripts in this chapter.

If you are using a version of PHP earlier than 4.3.0, go to the Boutell Web site at and download the source of the GD library. Follow the instructions included with that software, and consult its manual if you have difficulties with the installation.

To enable GD, Linux/UNIX users must add the following to the configure parameters:

 --with-gd=[path to GD directory] 


If you are using the bundled version, you can omit the [path to GD directory] portion of the parameter.

After running the configure program again, go through the make and make install process as you did in Chapter 1. Windows users who want to enable GD simply have to activate php_gd2.dll as an extension in the php.ini file.

After making changes to PHP or the php.ini file, restart your Web server and look at the output of the phpinfo() function. You should see a section called "gd", and you're looking for something that says "GD support enabled."

By simply installing GD, or using the bundled library, you are limited to working with files in GIF format. However, by installing additional libraries, you can work with JPEG and PNG files as well.

  • Find JPEG library information at

  • Find PNG library information at To work with PNG files, you also need to install the zlib library, found at

Follow the instructions at these sites to install the libraries. Then Linux/UNIX users must re-configure PHP by adding the following to the configure parameters:

 --with-jpeg-dir=[path to jpeg directory] 


 --with-png-dir=[path to PNG directory] --with-zlib=[path to zlib directory] 

After running the configure program again, go through the make and make install process, then restart your Web server so the new PHP module is activated. Look again at your phpinfo() output, and you should see that your additional libraries have been enabled. For instance, in the "gd" section, you'll see "JPEG support enabled" if you successfully used --with-jpeg-dir at configuration time.

With GD and its accompanying libraries installed, you can move on to learning about the basic image functions in PHP.

PHP Essentials
PHP Essentials, 2nd Edition
ISBN: 1931841349
EAN: 2147483647
Year: 2002
Pages: 74 © 2008-2017.
If you may any questions please contact us: