安装/配置
在线手册:中文 英文
PHP手册

安装

要激活 GD 支持,配置 PHP 时加上 --with-gd[=DIR] ,DIR 是 GD 的基本安装目录。要使用推荐的绑定的 GD 库版本(首次绑定于 PHP 4.3.0),使用 --with-gd 。要编译 GD 库,需要libpnglibjpeg

在 Windows 中,需要将 GD2 的 DLL 文件 php_gd2.dll 作为一个扩展包含在 php.ini 中。GD1 的 DLL 文件 php_gd.dll 在 PHP 4.3.2 中被删除了。此外要注意首选的真彩色图像函数,例如 imagecreatetruecolor(),需要 GD2。

Note:

要在 Windows 下启用 exif,在 php.ini 中 php_mbstring.dll 必须在 php_exif.dll 之前加载。

要在 PHP 3 中禁止 GD 支持,在配置时加上 --without-gd

要增强 GD 的能力以处理更多的图像格式,在配置 PHP 时指定 --with-XXXX 的配置开关。

所支持的图像格式
图像格式 配置开关
jpeg-6b 要激活 jpeg-6b 的支持,加上 --with-jpeg-dir=DIR .
png 要激活 png 的支持,加上 --with-png-dir=DIR 。注意,libpng 需要 zlib library,因此配置中还要加上 --with-zlib-dir[=DIR]
xpm 要激活 xpm 的支持,加上 --with-xpm-dir=DIR 。如果配置时提示找不到所需要的库,可以加上到 X11 库的路径。

Note: 当把 PHP 和 libpng 一起编译时,必须使用和 GD 库连接的同一个版本。

要增强 GD 的能力以处理更多的字体,在配置 PHP 时指定 --with-XXXX 的配置开关。

所支持的字库
字库 配置开关
FreeType 1.x 要激活 FreeType 1.x 的支持,加上 --with-ttf[=DIR]
FreeType 2 要激活 FreeType 2 的支持,加上 --with-freetype-dir=DIR
T1lib 要激活 T1lib(Type 1 字体),加上 --with-t1lib[=DIR]
本地 TrueType 字符串函数 要激活本地 TrueType 字符串函数的支持,加上 --enable-gd-native-ttf


安装/配置
在线手册:中文 英文
PHP手册
PHP手册 - N: 安装

用户评论:

Regular Guy (05-Feb-2012 12:51)

CentOS 6

yum install php-gd

worked, no need to recompile.

drummerpatch at gmail dot com (29-Jan-2012 05:45)

Running a Debian Squeeze server, all I had to do to add support for GD to my Apache server was simply run `apt-get install php5-gd` and then restart Apache. No recompiling or reconfiguring or anything. Simple!

florian (31-Jul-2011 10:50)

People having difficulties setting the --with-libdir option needs to set it like this :

--with-libdir=lib64

That option works fine. Doing --with-libdir=/usr/lib64 doesn't work because the configure script prepends the --prefix option before the --with-libdir option. So, doing --with-libdir=/usr/lib64 makes the configure script to look for libs in /usr/usr/lib64, which is wrong of course, and not in /usr/lib64.

Joseph Marlin (23-Jun-2011 02:07)

I also had to install the libgd package in addition to php5-gd:

sudo apt-get install libgd2-xpm-dev*

octopus at logicaloctopus dot com (17-Apr-2011 08:07)

I was running PHP on a SUSE box on amazon EC2.
to get gd to work all I had to do was to run:
> yast -i php5_gd

and when that completed I just restarted apache. voila!

boly38 at gmail dot com (17-Mar-2011 01:25)

To get GD bundled under Ubuntu, the way to recompile php5 with gd is described here (french) http://doc.ubuntu-fr.org/modules_php#php5-gd

lucaferr (21-Dec-2010 01:30)

In Slackware 64bit (I tested 13.1) you need to make symlinks to libpng and libjpeg if you want to compile php with GD, even if you've already installed them, otherwise configure will give you a "libpng not found" error, even if you set --with-libdir=/usr/lib64 (it seems to have no effects at all). So, run these before configuring:
ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so
ln -s /usr/lib64/libpng.so /usr/lib/libpngp.so

elassoto at hotmail dot com (10-Sep-2010 09:54)

If you have already compiled PHP and want to recompile '--with-gd', don't forget to run 'make clean' first!

Larry Kluger (13-Jan-2010 10:40)

On 64bit centos, needed to create links from lib64 to lib. Php seems to insist on looking in lib, even with "with-jpeg-dir" config.

Eg

ln -s /usr/lib64/libjpeg.so.62.0.0  /usr/lib/libjpeg.so
ln -s /usr/lib64/libpng.so.3.10.0  /usr/lib/libpng.so

./configure  \
  --with-config-file-path=/etc \
  --prefix=/usr \
  --with-bz2 \
  --enable-zip \
  --enable-mbstring \
  --with-apxs2=/usr/sbin/apxs \
  --with-zlib-dir=/usr/include \
  --with-libxml-dir=/usr/lib64 \
  --with-mcrypt=/usr/lib64 \
  --with-mysql \
  --with-gd \
  --with-jpeg-dir=/usr \
  --with-png-dir=/usr \
  --enable-calendar

ben at bluehype dot com dot au (27-Jan-2009 12:07)

If you have already compiled php without jpeg support and decide you want it , then before you compile again you might need to run "make clean" first.

Naveen Kumar (06-Jan-2009 07:08)

For Redhat Linux 64 bit machine:
--with-libdir=lib64 --with-gd  --with-jpeg-dir --with-png-dir

msc at homofaber dot com (30-Oct-2008 03:45)

It should be noted that it might be pretty easy to install gd without the need to recompile php, when using debian:

apt-get install php5-gd

gerard at charlesconsulting dot com (21-Aug-2008 05:36)

On my system,I had to ensure I had not only libpng and libjpeg, but also the libpng-devel and libjpeg-devel packages installed.

kronn (17-Jul-2008 10:44)

Everytime I try to install the gd-lib as a dynamic extension, I run into the same problem: It wont work.

I get the following error:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/pkg/lib/php/20040412/gd.so' - dlopen(/usr/pkg/lib/php/20040412/gd.so, 9): Symbol not found: __cg_jpeg_resync_to_restart\n  Referenced from: /System/Library/Frameworks/ApplicationServices. framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO\n  Expected in: /usr/pkg/lib/libjpeg.62.dylib\n in Unknown on line 0

I use pkgsrc for software package managment on my mac, as may be apparent from the paths.

The solution is, to edit the file /usr/pkg/sbin/envvars and remove or comment the DYLD_LIBRARY_PATH because it is causing the error...

mike at clove dot com (18-Apr-2008 05:40)

jpeg [jpg] file processing doesn't work unless you build php with '--with-jpeg-dir=<path to jpeg library>'.

It still doesn't work unless <path to jpeg library> points to the directory holding the directory holding the library unless libjpeg.<whatever> is in either /usr/lib or /usr/local/lib. For instance, on a mac with libjpeg.a installed via mac ports, you need '/opt/local' rather than '/opt/local/lib'