Adding GD to PHP on OS X 10.5.5 Leopard (Desktop)

Posted on 18th November 2008 in apache, development, OSX, php

Introduction

I am trying to help out on a cakePHP project for a friend. One of the things that has been added recently was a phpCaptcha component. This component uses the GD module to create images using text. Of course everything works fine on the test website, but not on my machine.
I am working on a MacBook Pro, Intel Core 2 Duo running Mac OS X Version 10.5.5.

This is mainly for my own memory, as it took me a few hours to get this working, when it should have only taken a few minutes.

Disclaimer

If anyone actually finds this page and follows the instructions on it, you do so at you’re own risk. Backup your system before you start. Please follow the information provided by topicdesk.com. The information here worked on my system, on 18th November 2008, it may or may not work for you. If something goes wrong, I will not be able to fix it for you. I will accept no responsibility for any use of this information.

Background Research

There are a number of tutorials out there that already deal with this issue, the ones that I found were either slightly out of date, or missed something. So rather than have to go through the discovery process again I am leaving myself a note here.

Let me stress that point, this is a note for myself. Other people who know far more about OS X/Apache/PHP than me have written excellent articles, which have helped me get to this point.

First of all here are some links to people who have already written about the process.

  • topicdesk.com – A pdf that contains the instructions for adding the GD Extensino to PHP5 on OS X Server 10.5.x
  • Kenior Design – Good instructions with a decent level of detail. Interesting conversations in the comments.
  • 90kts.com – A shorthand version of the topicdesk pdf. The comments to this post helped me get to my solution.

The instructions found in the PDF from topicdesk.com got me as far as installing GD and editing my php.ini file, which meant that GD was listed in phpinfo(). (Instructions on how to use phpinfo are in the pdf from topicdesk.com)

At this point I thought that I was home and dry, however this wasn’t the case. When trying to access a captcha image I was still not seeing anything. Checking my apache error log (tail /var/log/apache2/error_log), I discovered this error message.

The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug

Which lead me to these pages that describe what is going on.

With all these sources of information at hand I went ahead and tried to follow the steps outlined, and failed. The mistakes were simple ones, but for someone who is not used to working from the OS X command line, I am sure they are not uncommon.

These are the mistakes that I made: -

  • When building the various downloads, ensure that you are building for the correct architecture, or build for multiple platforms. My MacBook Pro has an Intel Core 2 Duo, which is a 64 bit CPU not a 32 bit.
  • Before downloading any of the source code that is listed in these posts, ensure that you are downloading the version appropriate to your installation.
    1. I am using OS X 10.5.5, most of the references have links to 10.5.4 or earlier.
    2. My PHP installation is 5.2.6, again the reference material talks about 5.2.5 or earlier.

Successful Installation

So these are the command lines instructions that I used to successfully install GD into PHP 5.2.6 on Mac OS X Leopard 10.5.5 (Most of this is identical to the PDF from topicdesk.com, with changes to the download locations). I am including these steps here merely for completeness. Please refer to their PDF for more information: -

Installing libjpeg was exactly how the topicdeskpdf explained.

mkdir -p /SourceCache
cd /SourceCache

curl -O http://www.ijg.org/files/jpegsrc.v6b.tar.gz
tar xzpf jpegsrc.v6b.tar.gz

cd /SourceCache/jpeg-6b
cp /usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .

(This is what I used as it I am installing for my 64 bit CPU)

MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp” CCFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe” CXXFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe” LDFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load” ./configure –enable-shared

make
sudo mkdir -p /usr/local/include
sudo mkdir -p /usr/local/bin
sudo mkdir -p /usr/local/lib
sudo mkdir -p /usr/local/man/man1
sudo make install

(Deviation from the topicdesk.comPDF)
Now we need to get a newer version of FreeType so that we don’t see that horrible error. I chose to use a version from this location http://download.savannah.gnu.org/releases/freetype/ specifically the 2.3.7 version.

cd /SourceCache
curl -O http://download.savannah.gnu.org/releases/freetype/freetype-2.3.7.tar.gz
tar xvfp freetype-2.3.7.tar.gz
cd freetype-2.3.7

This is the important line as it re-compiles FreeType with these options –with-fsspec=no –with-fsref=no –with-quickdraw-toolbox=no –with-quickdraw-carbon=no
This prevents FreeType from causing the Fork() error.

MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp” CCFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe” CXXFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe” LDFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load” ./configure –with-fsspec=no –with-fsref=no –with-quickdraw-toolbox=no –with-quickdraw-carbon=no

make
sudo make install

Then we get back to the topicdesk PDF. Again my versions of PHP and OS X are different from the ones that they are referring to, so I went looking here http://www.opensource.apple.com/darwinsource/10.5.5/ to find the right code to download, and found http://www.opensource.apple.com/darwinsource/10.5.5/apache_mod_php-44.1/

cd /sourcecache
curl -O http://www.opensource.apple.com/darwinsource/10.5.5/apache_mod_php-44.1/php-5.2.6.tar.bz2
tar xjf php-5.2.6.tar.bz2
cd php-5.2.6/ext/gd

sudo phpize

This next line introduces another change, in that I am now linking to the newly compiled FreeType library, not the pre-installed version. (–with-freetype-dir=/usr/local/lib)

MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp” CCFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe” CXXFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe” LDFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load” ./configure –with-zlib-dir=/usr –with-jpeg-dir=/usr/local/lib –with-png-dir=/usr/X11R6 –with-freetype-dir=/usr/local/lib –with-xpm-dir=/usr/X11R6

make
sudo make install

sudo apachectl graceful

Finished! Wasn’t so bad after all. Remember the configure options that I have used here are because I am compiling on a 64 bit CPU. I imagine that I could change some of those compilation options to simply create the 64 bit option, but as I was following other tutorials to get this done, I didn’t want to stray off the path even further than I had to.

comments: Closed

4 Responses to “Adding GD to PHP on OS X 10.5.5 Leopard (Desktop)”

  1. Lori says:

    Hello Aiden,
    Thanks for the script. It looks like I have the same configuration as you so I went through your steps and everything seemed to download and compile very nicely!
    I have a new folder on my Hard Drive Called SourceCache with six items – the Freetype 2.3.7, the jpeg-6b and php 5.2.6 and their respective .tar files.

    I then ran <?php phpinfo(): ?> to see the new configuration. It still looked like the old without the GD or freetype module.

    I think I may have done something wrong.
    My Apache2 and php.ini files are in the default delivered MAC location – HD:Private:etc:

    Should I have been there (cd /Private/etc) before I started with the first mkdir -p /SourceCache command in terminal? If so can I move the files from HD:SourceCache to HD:Private:etc, or should I delete and go through install and compile process?

    Any light you could shed would be wonderful. Thanks in advance.

    Lori

  2. Adam says:

    Great tutorial, worked a charm. Thanks.

    A couple of corrections – the Freetype command in your post doesn’t copy and paste as it contains characters that don’t execute in the shell. The following works:

    MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load" ./configure -with-fsspec=no -with-fsref=no -with-quickdraw-toolbox=no -with-quickdraw-carbon=no

    For the benefit of others, before restarting Apache don’t forget to add to /etc/php.ini:

    extension=gd.so

    And comment out:

    extension_dir = "./" by placing a semicolon in front of it.

    Thanks again,
    Adam

  3. Apologies for not replying to these comments any earlier. I got a little busy during December and January.

    Lori – I will have another look at my configuration and let you know where my Apache2 and php.ini files live.

    Adam – Thanks for the corrections :) I wonder if this helped out Lori at all?

  4. Phil says:

    The phpCaptcha component has some serious security flaws.

    http://www.idontplaydarts.com/2011/05/exploit-phpcaptcha-securimage/



Pings responses to this post