PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Compiling and Enabling GD in PHP 4.3
Pages: 1, 2

Installing JPEG Support

The JPEG library is, perhaps, the easiest component of the GD library to configure and install. I can safely say I've never had any problems with it.

Once you have downloaded the source code, you need to compile and install it:

/marcot/code/jpeg-6b# ./configure --prefix=/usr 
    && make && make install

JPEG has very few external dependencies and the compilation and installation should go through without a hitch. At this point, you're ready to go back to your PHP source tree and re-run configure:

/marcot/code/php-4.3.0# ./configure --with-gd -with-zlib-dir=/usr/include
    --with-jpeg-dir=../jpeg-6b /marcot/code/php-4.3.0# make clean && 
    make && make install

This should result in your PHP engine being neatly reconfigured, compiled, and installed with no problems of any sort.

Getting TrueType Fonts to Work

As I mentioned earlier, the easiest way to provide TrueType rendering capabilities to your scripts is to use the built-in GD library, which requires the FreeType library in order to function properly.

The best way to tell if FreeType is installed on your machine is to check for the freetype.h file. For example, on my machine, I get these results:

/marcot/code/# locate freetype.h

As you can see, both the FreeType and FreeType2 libraries are installed on my server. This is a fairly common result for a RedHat 7.2 or higher machine, as FreeType has been integrated in the X environment to provide support for high-quality TrueType rendering.

If FreeType is not installed on your system, all you need to do is download it, compile it, and install it. You will, however, have to make a decision as to whether you want to compile your version of FreeType with the hinting code that infringes on Apple's patents (for which you will need to buy a license from Apple itself) or not. If you decide to exclude these features, first edit the file ft_conf.h in the FreeType source tree's main directory. In version 1.3.1 of the library, line 98 of that file contains the following:


To disable the patent-sensitive functionality and save yourself from having to buy a license, change it as follows:


Save the file, and proceed with the configuration and compilation of the library as you would normally:

/marcot/code/freetype-1.3.1# ./configure --prefix=/usr 
    && make && make install

Once FreeType is installed on your system, the PHP configuration script will automatically determine its location. Enabling the built-in TrueType GD functionality only requires the use of a simple switch:

/marcot/code/php-4.3.0# make clean && ./configure 
    --with-gd --with-zlib-dir=/usr/include --enable-gd-native-ttf 
    && make && make install

Depending on the exact version of PHP that you have, the configuration script may not be able to correctly identify the last switch because of a misspelling that existed in its code at some point (and that was fixed a while ago). If --enable-gd-native-ttf fails, try --enable-gd-native-tt instead (without the final f).

If you'd rather use the FreeType library without taking advantage of the built-in TrueType functions, use the following configuration switches:

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

The installation of support for the FreeType2 library works in a very similar way--you can, in fact, compile both libraries in the GD extension at the same time. This can be useful if some of your older scripts still use FreeType, but you want to use FreeType2 with the new ones, or if you are a hosting provider and want to let your customers use both.

Like its predecessor, FreeType2 is easy to configure and install. Since it does not use any of the patented code from Apple, you don't even have to change any of its source files:

/marcot/code/freetype-2.1.3# ./configure --prefix=/usr 
    && make && make install

FreeType2 support in the GD extension is enabled through a simple command-line switch:

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

Compiling Support for Type1 Fonts

The T1Lib library is used by the GD extension to provide support for PostScript Type1 fonts. This format, very popular among professional typesetters, provides the highest output quality, although the fonts that use it are often expensive. (The X Window System includes a handful of free Type1 fonts.)

As with TrueType, the best way to compile T1Lib into your PHP interpreter is to first verify whether it's already available on your system:

/marcot/code# locate t1lib.h

If this doesn't work for you, you will need to download and compile it. There are no special requirements for this task. The process is quite straightforward:

/marcot/code/t1lib-1.3.1# ./configure --prefix=/usr 
    && make && make install

As usual, to compile the library within PHP, you will only need a simple ./configure switch:

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

Putting It All Together

Having covered all of the different options that can be compiled into the GD extension, it's time to compile everything together and test that it works. A complete installation of the GD extension will look similar to the following:

/marcot/code/php-4.3.0# make clean && \
./configure --with-gd --with-zlib-dir=../zlib-1.1.4/ \
--with-jpeg-dir=../jpeg-6b --with-freetype=/usr/include/freetype \
--with-freetype2=/usr/include/freetype2 -with-t1lib=/usr/include && \
make && \
make install

This should result in a complete recompilation and installation of your PHP interpreter with support for GD and all of its ancillary technologies. You can test its functionality through this simple script:


function test ($string, $test)
    echo $string . '... ';

    if ($test)
        echo "success.\n";
        echo "fail.\n";

$token = "Testing GD!";

// Create image

$pic = ImageCreate (300, 300); 

test ('Creating image', $pic);

$col2 = ImageColorAllocate ($pic, 0, 0, 100); 
$col1 = ImageColorAllocate ($pic, 200, 200, 200); 

// Test PNG/JPG functionality

$res1 = @ImagePNG ($pic);
$res2 = @ImageJPEG ($pic);

test ('Testing PNG output', $res1);
test ('Testing JPEG output', $res2);

// Test TrueType functionality

$ttfont = trim (`locate -n 1 .ttf`);

test ('Testing FreeType', @ImageTTFText 
    ($pic, 30, 0, 10, 40, $col1, $ttfont, $token));
test ('Testing FreeType2', @ImageFTText 
    ($pic, 30, 0, 10, 40, $col1, $ttfont, $token, array()));

// Test Type1 functionality

$font = @ImagePsLoadFont (trim (`locate -n 1 .pfb`));

if ($font)
    $res = @ImagePsText ($pic, $token, $font, 10, $col1, $col2, 0, 0);

test ('Testing the Type1 library', ($font && $res));

test ('Destroying image', ImageDestroy($pic)); 


If everything goes according to plan, your PHP interpreter should be able to pass the test with flying colors:

Creating image... success.
Testing PNG output... success.
Testing JPEG output... success.
Testing FreeType... success.
Testing FreeType2... success.
Testing the Type1 library... success.
Destroying image... success.

Marco Tabini specializes in the introduction of open-source products in enterprise environments.

Return to the PHP DevCenter.

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: