If you want to do some graphics work within PHP you will need the GD library. It will take several steps of preparation before you can add this to PHP. First you need to download the source code from the Freshmeat libjpeg project.

First unpack the source code and then go into the newly created folder and execute the following commands:

cp /usr/share/libtool/config/config.sub .
cp /usr/share/libtool/config/config.guess .

Which will copy these files to the current directory. They are required for the configuration phase of this installation.

Next for 64 bits systems use:

export MACOSX_DEPLOYMENT_TARGET=10.6
CFLAGS="-arch x86_64" \
CXXFLAGS="-arch x86_64" \
LDFLAGS="-arch x86_64" \
./configure --enable-shared

For 32 bits systems use:

export MACOSX_DEPLOYMENT_TARGET=10.6
./configure --enable-shared

Next follow with the commands:

make
sudo make install

To compile and install the library.

Next step is to recompile the php installation as described on this page:
Compiling PHP in 64 bits or Compiling PHP in 32 bits depending on your requirements.

The only change you need to make is to add:

--with-gd \
--with-jpeg-dir=/usr/local/lib \
--with-png-dir=/usr/X11R6 \
--with-freetype-dir=/usr/X11R6 \
--with-xpm-dir=/usr/X11R6 \

To the configure statement and it will include this library for you.

For example when compiling in 32 bits use:

./configure --prefix=/usr/local/php5 \
  --mandir=/usr/share/man \
  --infodir=/usr/share/info \
  --sysconfdir=/etc \
  --with-config-file-path=/etc \
  --with-zlib \
  --with-zlib-dir=/usr \
  --with-openssl \
  --without-iconv \
  --enable-exif \
  --enable-ftp \
  --enable-mbstring \
  --enable-mbregex \
  --enable-sockets \
  --with-mysql=mysqlnd \
  --with-pdo-mysql=mysqlnd \
  --with-mysqli=mysqlnd \
  --with-gd \
  --with-jpeg-dir=/usr/local/lib \
  --with-png-dir=/usr/X11R6 \
  --with-freetype-dir=/usr/X11R6 \
  --with-xpm-dir=/usr/X11R6 \
  --with-apxs2=/usr/local/apache2/bin/apxs

By the way if you run into the error:

checking for jpeg_read_header in -ljpeg... yes
configure: error: libpng.(a|so) not found.

You didn’t install X11 when you installed OS X. You can still install it using the “optional installs” and select X11 from Applications and install it on the default location.