GD for PHP on OS X Leopard

Before rambling off into a long story, the easy solution is here: http://www.entropy.ch/software/macosx/php/

Marc Liyanage has done an excellent job putting together a package that will update your OS X installed Apache to use his PHP module configured with most of the common extensions.  As noted by the name of this post, GD was the missing extension which lead me to find this great install.  The only caveat is if you ever run php from the command line, you need to specify the new path if you want to use the version he installs… you’ll understand why later.

The Background Plot…

First off I’ll let everyone out there in on a big secret, for most of us, there are 2 flavors of PHP on your system. The command line executable, and the Apache PHP module. Updating one and not the other will lead to massive frustration as you try to figure out why on the command line PHP indicates it sees the GD extension, but when you try a phpinf() GD is not reported. I’m going to try and take us through getting both working with GD.

So, as anyone trying to use Drupal on an OS X box knows, neither the php executable nor the php apache module comes configured with GD.  You’ll know that there are numerous ways to work around this problem posted on the web (and may actually get one to work, but then you probably wouldn’t be reading this…).   However, none of these fixes fully explain the issue of why GD isn’t built in and what must be accomplished before the version of php Apache is using will recognize the GD library.   This makes it difficult for the unsuccessful people (like myself) to figure out why their attempted fixes weren’t working as the 40% or so lucky code monkeys who seemed happy.  Before enlightenment, the particual issue that killed me was that I could get php to recognize GD from the command line, not through Apache.

What the hell is GD and why is Drupal bothering me about it

GD is graphics library used to create and manipulate images.  I know this is sort of the overly generalized explanation you get like when you as someone ‘what’s a consultant do’, but it pretty much sums it up.  You can read about it from it’s download site http://www.libgd.org, but as all good open source projects are, it’s sparse on the what-the-heck-do-you-do-please-teach-me-O-wise-ones side.  So even though it does way more than this, just assume GD is a library that what many web apps use to resize pictures and create cute thumbnails.  It’s written in C, but can be wrapped up so php an perl can use it.

When PHP was born on your machine

First let’s look at the command line version (usually residing at /usr/bin/php). PHP is an executable file, just like grep, vi, or any other program on your box.  At some point in time it was compiled, in this case by Steve Jobs and his happy elves.  When the program was compiled, some things were compiled in, some left out.  For some reason, they felt that GD which is used all over the freaking place should be left out.  Maybe a licensing issue with a dependent library; maybe a security issue; maybe an off-color comment at some past Macworld; who knows, but why ask why.  PHP is pretty cool in the fact that if something was left out, it can be compiled as an extension for adding it on later without having to recompile  your entire PHP.

First what does your PHP think it has?

To find out what PHP has configured on the you pass it the  -i flag.  This dumps a huge amount of text, so if you’re best off to pipe it to something like grep to filter only the stuff you care about.   In this case, I want to know only about GD I’d go with:

$> php -i | grep -i gd

which results in:

GD Support => enabled
GD Version => bundled (2.0.34 compatible)

Sweet.

So basically, this tells me only 1 thing, when running php via the command line /usr/bin/php it’s aware of my GD library.  So what if you don’t get this nice result?  A couple things could be going on.  First check to see if you have a copy of gd.so in your extensions library.  If you’re using the default OS X php the path should be:

/usr/lib/php/extensions/no-debug-non-zts-20060613/

If it’s not their, check your php.ini file (in /etc) to see what is set for the extensions_dir.  I’m going to assume you have neither the gd.so library, or a extensions_dir that points to anything but “./”.
Luckily, compiling GD into a library is pretty easy, start with editing your php.ini (this is where you can tell php to look for stuff when it runs):

php.ini

  1. cd /etc
  2. vim php.ini (assuming your user can edit this) and change settings appropriately. Change:
        extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20060613"

    You must set the extension dir correctly, or php will use it’s default value–whatever that is!

GD (stolen from http://akrabat.com/2009/03/05/setting-up-php-on-os-x-leopard/)

Installing GD onto the stock PHP install that is supplied with OS X is slightly more complicated than you’d expect because you need to install libjpeg first.

Libjpeg

Libjpeg is available from the Independent JPEG Group.

  1. Download the source code: http://www.ijg.org/files/jpegsrc.v6b.tar.gz
  2. extract to ~/src
  3. cd ~/src/jpeg-6b
  4. cp /usr/share/libtool/config.* .
  5. ./configure --enable-shared
  6. sudo make install
  7. Libjpeg is now installed in /usr/local/lib

GD extension

  1. cd ~/src/php-5.2.6/ext/gd
  2. phpize
  3. MACOSX_DEPLOYMENT_TARGET=10.5 \CFLAGS='-O3 -fno-common -arch i386' \

    LDFLAGS='-O3 -arch i386' \

    CXXFLAGS='-O3 -fno-common -arch i386' \

    ./configure --with-zlib-dir=/usr --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/X11R6 --with-freetype-dir=/usr/X11R6 --with-xpm-dir=/usr/X11R6

  4. make
  5. sudo make install

Edit /etc/php.ini and find the extension_dir line and add after it:

    extension=gd.so

At this point you should be able to try and query php to confirm it’s kosher with the new GD libraries:

$> php -i | grep -i gd

which results in:

GD Support => enabled
GD Version => bundled (2.0.34 compatible)

If you don’t get this, you hosed something–most likely the compile flags for GD.  These are system dependent, so I’m assuming you’re running OS X 10.5 or up.  Great, now if you want to call the GD libraries from a php script from the command line your set.  What about via Apache?  Nope. Remember, the Apache PHP module is separate from the the command line executable.

To Be Continued…

1 comment to GD for PHP on OS X Leopard

  • Jay

    Hi I am having the same problem gd and mcrypt is showing enabled in the command line but does not report in php info (the link you provided above is broken). How did u solve?

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>