PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to PHP Subscribe to Newsletters

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
/usr/share/texmf/doc/help/Catalogue/entries/freetype.html
/usr/include/freetype1/freetype/freetype.h
/usr/include/freetype2/freetype/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:

/* #undef   TT_CONFIG_OPTION_NO_INTERPRETER */

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

#define   TT_CONFIG_OPTION_NO_INTERPRETER */

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/ 
    --with-freetype2=/usr/include/freetype2

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
/usr/include/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/ 
    --with-t1lib=/usr/include

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:

<?php

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

    if ($test)
        echo "success.\n";
    else
        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

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

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.


Have you any other tips for compiling GD with PHP? Share them here.
You must be logged in to the O'Reilly Network to post a talkback.
Post Comment
Full Threads Oldest First

Showing messages 1 through 20 of 20.

  • What i do wrong?
    2007-03-29 00:07:55  luck00 [Reply | View]

    In my browser apear something like that

    Creating image... success. �PNG  ��� IHDR��,��,���C�6���PLTE��d��� ����"IDATh���1��� �Om ?������������x-���{�����IEND�B`�

    What i do wrong?
  • Regarding GD Library for php
    2007-03-12 23:51:06  bhavi [Reply | View]

    I have installed in my pc PHP,GD,MYSQL and all are working but its gives output for any GD script in netscap navigator.in mozilla and firefox or in IE it does not support.so,For GD WHat i have to do that i can get its benifit
  • avoiding long configuration line
    2007-02-22 10:21:21  webofunni [Reply | View]

    if we use --prefix=/home/user/gdlib in gd configuration and make the install to that dir we can avoid the long line in php configuration by

    ./configure --with-gd=/home/user/gdlib

    it's worked for me hope it will work for you also
  • imagettftext(): Could not find/open font
    2005-04-04 06:41:43  aldo123 [Reply | View]

    I've installed the GD as this excellent tutorial indicates but I still having the following warning message:

    Warning: imagettftext() Could not find/open font

    Does anyone know how to resolve this tricky issue?

    Thanks in advance.
  • Getting the new PHP recognized!
    2004-03-27 23:11:02  kayaker [Reply | View]

    I followed the excellent tutorial, but now that PHP is in the usr directory, how do I get apache to see it there and not in it's old location?
    • Getting the new PHP recognized!
      2007-02-22 10:30:32  webofunni [Reply | View]

      While building php you can use
      ./configure --with-gd --with-apxs=/pah-to-apache/apache/bin/apxs
      it will work if you enabled module while configuring apache
      ie /path-to-apache/apache# ./configure --enable-module=so
      otherwise you need to rebuild apache
  • php compiling
    2004-03-10 15:57:26  gheos [Reply | View]

    I need to run ./configure to be able to compile php so the image functions work....
    How do I do this? Where do I type that ./configure.... and all the other commands mentioned everywhere? where do I type these commands....?? I'm totally new to the compiling business so... help me pleaz..
    • php compiling
      2007-02-22 10:37:39  webofunni [Reply | View]

      first you had to get php.x.x.x.tar.gz or any other compressed version for unix (linux)(go to www.php.net) then unpack it to a dir say php.4.4.4 (think it is in /home/me/php.4.4.4/ ) then in the terminal type
      cd /home/me/php.4.4.4 then type ./configure Hope you got it
  • Jpeg not suppotred in php 4.3.4
    2004-01-13 01:47:47  anonymous2 [Reply | View]

    Hi!

    I have a problem with installing php 4.3.4 with GD library.

    I don't have jpeg support.

    Here's configure line:
    ./configure --with-mysql --with-gd --with-openssl --enable-gd-native-ttf --with-ttf --with-t1lib --enable-ftp --enable-calendar --with-bz2 --with-apxs2=/usr/local/apache2/bin/apxs --with-zlib --with-jpeg-dir=/usr/lib

    Please help.

    Regards,

    Urban
  • Many Thanks for this article.
    2003-08-27 10:20:25  anonymous2 [Reply | View]

    Not having too much experience with the Linux platform per se not to mention, any experience w/ installation of PHP, you gave me the initial bump to complete this imperfect task of setting up PHP on my RHat 9 box without losing all hope and my mind. Thanks again. I hope that in time, I will be able to bail out someone in the Open Source Epidemic as you have done for me. Peace & Thanks.
  • OK, You totally rule
    2003-08-16 13:58:40  anonymous2 [Reply | View]

    Except it took me four hours of banging my head up against manuals, discussion groups and google before I found this article. And you have solved my problem and PHP now has Freetype working. Jeez. Thank you thank you thank you!

    neil AT bitey DOT com
  • Not a tip, but a question
    2003-04-01 06:46:00  anonymous2 [Reply | View]

    I'm already running 4.2.3. I am interested in UPGRADING my current setup, not blowing it away and restarting from scratch. This article (as do seemingly all such documentation) assumes that I do NOT have php currently installed and running.

    A little help, here?
    • Not a tip, but a question
      2004-04-14 12:08:55  amaiman [Reply | View]

      As far as I know, there is no upgrade path.

      You have to "blow it away and start from scratch".
    • Not a tip, but a question
      2003-04-01 07:03:41  anonymous2 [Reply | View]

      Simply download the new version and copy your config.nice from the old one---that usually does the trick for me :)
      • Not a tip, but a question
        2003-04-01 10:17:16  anonymous2 [Reply | View]

        "copy" config.nice ... and then what? ./configure --with-config=config.nice ? make config=config.nice? Simply run ./configure ?
        • Not a tip, but a question
          2003-04-01 10:28:20  anonymous2 [Reply | View]

          Nebber mind. I RTFM ...er, RTFscript. :)
  • Problems with not seeing libjpeg.a
    2003-03-28 10:19:17  anonymous2 [Reply | View]

    Hi, first just wanted to say thanks so much for creating this article. I have been looking for something like this everywhere, and I knew O'Reilly would pull through. When trying to configure PHP with the jpeg-6b package installed, even after pointing to the right directory, PHP fails to see the libjpeg.a file in the directory. Do you have any ideas why this could be happening? Thanks again for a great article.
    • What i do wrong?
      2007-03-29 00:04:28  luck00 [Reply | View]

      In my browser apear something like that

      Creating image... success. �PNG  ��� IHDR��,��,���C�6���PLTE��d��� ����"IDATh���1��� �Om ?������������x-���{�����IEND�B`�

      What i do wrong?
    • Problems with not seeing libjpeg.a
      2003-04-01 20:36:41  anonymous2 [Reply | View]

      I had the same problem and it turns out that you have to compile libjpeg with both static and shared libraries:

      ./configure --enable-shared --enable-static --prefix=/usr && make && make install

      and then point to --with-jpeg-dir=/usr/lib in your PHP configure.

      Hope this helps.
    • Problems with not seeing libjpeg.a
      2003-04-01 17:03:56  anonymous2 [Reply | View]

      I am also have in this same problem except with PHP 4.3.1 instead.

      I am not sure what it is. Any help would be appreciated.

      Thanks.


Recommended for You

  1. Cover of PHP Anthology
    PHP Anthology
    Print: $39.95
  2. Cover of Mastering Regular Expressions
    Mastering Regular Expressions
    Print: $44.99
    Ebook: $35.99
  3. Cover of Learning PHP 5
    Learning PHP 5
    Print: $29.95
    Ebook: $23.99
  4. Cover of Upgrading to PHP 5
    Upgrading to PHP 5
    Print: $29.95

Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

gd

Articles that share the tag gd:

Compiling and Enabling GD in PHP 4.3 (14 tags)

View All

linux

Articles that share the tag linux:

Managing Disk Space with LVM (74 tags)

Use Your Digital Camera with Linux (60 tags)

mdadm: A New Tool For Linux Software RAID Management (59 tags)

Asterisk: A Bare-Bones VoIP Example (43 tags)

View All

install

Articles that share the tag install:

Installing Debian (17 tags)

Everything You Need to Know to Install Tiger (7 tags)

Multiuser Subversion (7 tags)

Compiling and Enabling GD in PHP 4.3 (5 tags)

Managing Disk Space with LVM (4 tags)

View All

programming

Articles that share the tag programming:

Rolling with Ruby on Rails (1374 tags)

Very Dynamic Web Interfaces (279 tags)

Ajax on Rails (231 tags)

Understanding MVC in PHP (202 tags)

A Simpler Ajax Path (186 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com