13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

484 Chapter 22 Generating Images<br />

Setting Up Image Support in <strong>PHP</strong><br />

Some of the image functions in <strong>PHP</strong> are always available, but most of them require the<br />

GD2 library. Detailed information about GD2 is available at http://www.libgd.org/<br />

Main_Page.<br />

Since <strong>PHP</strong> 4.3, <strong>PHP</strong> comes with its own forked version of the GD2 library,<br />

supported by the <strong>PHP</strong> team.This version is easier to install with <strong>PHP</strong> <strong>and</strong> is usually<br />

more stable, so it’s advisable to use this version. Under Windows, PNGs <strong>and</strong> JPEGs are<br />

automatically supported as long as you have the php_gd2.dll extension registered.You<br />

can do this by copying the php_gd2.dll file from your <strong>PHP</strong> installation directory (in the<br />

\ext subfolder) to your system directory (the C:\Windows\system directory if using<br />

Windows XP).You must also uncomment the following line in your php.ini file by<br />

removing the “;” at the beginning of the line:<br />

extension=php_gd2.dll<br />

If you have Unix <strong>and</strong> want to work with PNGs, you need to install libpng from<br />

http://www.libpng.org/pub/png/ <strong>and</strong> zlib from http://www.gzip.org/zlib/.<br />

You then need to configure <strong>PHP</strong> with the following options:<br />

--with-png-dir=/path/to/libpng<br />

--with-zlib-dir=/path/to/zlib<br />

If you have Unix <strong>and</strong> want to work with JPEGs, you need to download jpeg-6b <strong>and</strong><br />

recompile GD with JPEG support included.You can download it from ftp://ftp.uu.net/<br />

graphics/jpeg/.<br />

You then need to reconfigure <strong>PHP</strong> with the following option <strong>and</strong> recompile it:<br />

--with-jpeg-dir=/path/to/jpeg-6b<br />

If you want to use TrueType fonts in your images, you also need the FreeType library. It<br />

also comes with <strong>PHP</strong> since version 4. Alternatively, you can download it from<br />

http://www.freetype.org/.<br />

If you want to use PostScript Type 1 fonts instead, you need to download t1lib,<br />

available from ftp://sunsite.unc.edu/pub/Linux/libs/graphics/.<br />

You then need to run <strong>PHP</strong>’s configure program with<br />

--with-t1lib[=path/to/t1lib]<br />

Finally, you will, of course, need to configure <strong>PHP</strong> using --with-gd.<br />

Underst<strong>and</strong>ing Image Formats<br />

The GD library supports JPEG, PNG, <strong>and</strong> WBMP formats. It no longer supports the<br />

GIF format. Let’s briefly look at each of these formats.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!