PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Compiling and Enabling GD in PHP 4.3

by Marco Tabini
03/27/2003

The release of version 4.3 of PHP caused a collective sigh of relief from developers who rely on the GD library extension to provide image-manipulation capabilities to their scripts. GD is very powerful, but it can be a real pain to configure and install when compiling the PHP interpreter. Luckily, the latest incarnation of PHP included a built-in version of the library that was supposed to make things easier for everyone.

Unfortunately, this is not necessarily the case. Unless your server is set up in just the right way, if you download the PHP 4.3 source tree from the PHP website and try to compile the GD library into it, you'll still be greeted by a nice set of error messages. My goal with this article is to share my experience in setting up and installing GD and, hopefully, to help you avoid some of the headaches I've had to endure in the process.

GD Components

The problem is that the extension itself is really made of several different components, not all of which are actually included in the PHP distribution package.

The GD library provides the functions necessary to manipulate images, such as by drawing on them, but is unable to convert them to a specific format, say PNG or JPEG, without the help of external libraries. Similarly, GD can draw simple bitmap fonts on a canvas through its own internal functionality, but using TrueType or PostScript Type 1 fonts requires external components as well.

In order to manipulate images in different formats, you will need to download one or more of the following libraries:

FormatLibrary
PNGlibpng
zlib
JPEGjpeglib
XPMlibXpm

Officially, the GD library does not support the GIF format any longer, due to the well-known patent issues that surround it. These days, the PNG format, which is free and generally more efficient than GIF, is the preferred image format of the open source community. However, it's probably fair to say that most web sites still use GIFs. While the ability to read and write PNGs will make it possible for your script to produce images that are compatible with practically all recent browsers, the version of GD that's built into the PHP interpreter still retains the ability to read GIF files--without requiring any external libraries.

If your goal, on the other hand, is to write beautiful text, you may want to install the following libraries:

FormatLibrary
TrueTypeFreeType
TrueTypeFreeType
TrueTypeBuilt-in (requires FreeType)
Type 1 (PostScript)T1Lib

As you can see, there are three different options if you want to be able to display TrueType fonts, and each of them has advantages and drawbacks.

Related Reading

Web Programming CD Bookshelf
Six Bestselling Books on CD-ROM
By A publication of O'Reilly Media

The built-in TrueType functionality is an excellent "quick solution" to the problem of typesetting TrueType fonts. It still requires the presence of the FreeType library on your system, but not of the source code itself. You could easily just install the library from your favourite Linux distribution and simply link the PHP interpreter against it. Chances are that it's already there, if you're running a relatively recent version of X.

FreeType and FreeType2 are two open source typesetting libraries that offer excellent TrueType-rendering capabilities. FreeType is a bit more problematic, because it uses a glyph optimization algorithm that is patented by Apple Computers. As a result, to use FreeType legally, you would either have to pay a royalty to Apple or disable the optimization engine and suffer a lower quality when rendering your fonts, particularly at small point sizes.

FreeType2, on the other hand, does not use any patented optimization techniques. As such, it offers the best possible quality available to render high-quality text.

A Couple of Important Notes

When illustrating compilation examples, I will be listing only ./configure switches necessary for the compilation of the GD extension and its related components. Naturally, you should add any other switches that are required for your particular version of PHP (such as APXS, FTP, and so on). Also, I will reference directories and files outside of the PHP tree as they appear on my computer. Although these may be different on yours, if you follow the instructions, you shouldn't have any problem locating them.

In addition, please don't forget to clean up the PHP tree whenever you run ./configure. I know it's annoying to recompile everything every time. It's also very safe, however, and my experience is that I tend to encounter fewer problems when I do so. You can usually clean the source tree by invoking make as follows:

/marcot/code/php-4.3.0# make clean

Getting Started

The first step towards making GD available to your scripts consists of downloading a copy of PHP 4.3. Besides the additional ease in installing a built-in version of the GD extension, there are many good reasons to upgrade to the latest version of PHP, including a number of bug and security fixes to make your applications safer.

Once you have the PHP source tree in your possession, start by configuring it:

/marcot/code/php-4.3.0# ./configure --with-gd

If the configuration script runs without a glitch, you're a really lucky bird. Chances are that you will receive a number of errors. For example, if the PNG library is installed on your system (and it probably is, if you're running X), configure will find it and try to compile it in:

If configure fails try --with-jpeg-dir=<DIR>
configure: error: PNG support requires ZLIB. Use --with-zlib-dir=<DIR>

As you can see, the script is complaining that we didn't provide it with a location where it could find the ZLIB library, which is required by the PNG component of GD. Wait a moment ... where does the PNG library come from? Simple--it's enabled by default in the source tree whenever you enable GD compilation.

If, on the other hand, PNG is not on your system and you want to use it, you will need to download and install the PNG library. This is not a particularly complex task, but libpng does not use a configuration script. You must copy the appropriate Makefile from the scripts/ directory into the main source tree in order for the compilation to take place:

/marcot/code/php-4.3.0# cd /marcot/code/libpng-1.2.5
/marcot/code/libpng-1.2.5# cp scripts/makefile.linux Makefile
/marcot/code/libpng-1.2.5# make prefix=/usr && make install

If all goes well, the PNG library will be installed and you're on your way. Note that I'm forcing make to use /usr as the prefix for the installation path in order to ensure that the library is installed in the system-wide directories. I will do so with all of the packages that I install in the system. Once this is done, the PHP configuration script should automatically find your PNG library and its header files and compile with it.

The ZLIB library is, most likely, already installed on your computer. You may want to update it to the latest version, since older ones had a buffer overflow problem that could pose a security threat, under certain conditions. If you download the source tarball from the ZLIB web site and untar it, you can compile the library as follows:

/marcot/code/zlib-1.1.4# ./configure --prefix=/usr 
    && make && make install

Finally, you should be able to go back to the PHP source tree and run the configure script again:

/marcot/code/php-4.3.0# ./configure --with-gd 
    --with-zlib-dir=/usr/include

The script should, at this point, run all the way through and write the proper configuration files. You should be able to compile PHP without any problems. The PNG functionality should work just fine--but you still can't produce JPEG images.

Pages: 1, 2

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: