安装

要激活 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

User Contributed Notes

Parmjit Singh 22-Feb-2021 01:48
Installation on Pop-os No need for php5 or php7.0 etc:

sudo apt install php-gd
omni at ominsapien dot com 31-Jan-2021 04:04
Hello. Does GD image processing allow for color spaces / color profiles to be defined or maintained from the source image? For example, if I upload a jpg with ProPhoto RGB color space with the embedded profile, will all of the subsequent processed images that are output maintain the profile? Or is there no profile and or is it changed to sRGB?

Same question goes for ImageMagick.

Thank you so much!
tecknovice at gmail dot com 24-Mar-2020 08:36
currently is php7.2-gd

sudo apt install php7.2-gd
Gabriel Torres 09-Feb-2020 10:52
As of PHP 7.4, to enable GD you have to use --enable-gd instead of --with-gd
nordin74 11-Jan-2020 07:40
Since 7.4 replace "--with-gd" by "--enable-gd"
abdulbasitsaeed 19-Mar-2019 04:28
To install PHP GD extension on an Amazon Linux AMI server, with php 7:

sudo yum install php 70-gd

Do remember to restart Apache after the installation!

sudo service httpd restart
fabrizzio at webtux dot cl 19-Oct-2018 11:22
php --version if result is 7.2 version

sudo apt-get install php7.2-gd
remalsha at gmail dot com 13-Feb-2018 07:22
On AntergosOS ( Arch linux )

*first run >>php -v   and get current php version. In my case for PHP 7.1.14

      sudo pacman -S php71-gd  

then restart you apache server using,
    
      sudo systemctl restart httpd
Qussayyon Qamaron: Qusai.zf2@gmailcom 25-Mar-2017 05:07
#install under Ubuntu 16.04:
    * if you have access to PHP7.1:
        <sudo> apt install php7.1-gd && <sudo> systemctl restart apache2
    * if you have access to PHP7.0:
        <sudo> apt install php7.0-gd && <sudo> systemctl restart apache2

------------------------------------------------
Qussayyon Qamaron: Qusai.zf2gmailcom
theonestep4 at gmail dot com 13-Dec-2016 05:00
To install on CentOS:

yum install php-gd
Anonymous 03-Nov-2016 12:52
For all who use php 7 and ubuntu

sudo apt-get install php7.0-gd
Mike K 30-Mar-2016 07:16
On Ubuntu I was missing the libpng-dev, libjpeg-dev (and in my case) libwebp-dev libraries, all gettable with apt-get.

Also once those libraries are installed, including --with-png-dir --with-jpeg-dir --with-webp-dir, without any values (i.e. =/dir) appeared to be valid ... though admittedly I didn't try without (in case the configure script autodetected them).
svestka1999 at gmail dot com 22-Feb-2016 06:20
leyluj21 at gmail dot com 23-Oct-2014 09:58
If you are using PHPBREW , the extension is called gd.
So you do.
```
phpbrew ext install gd
```

It will compile the extension to the core.
james dot a dot munsch at gmail dot com 05-Sep-2014 10:19
sudo apt-get install php5-gd && sudo service apache2 restart

To install under Ubuntu 14.04.
alejo dot jm at gmail dot com 20-Jul-2012 06:47
On Macox lion with mac port just use:
sudo port install php5-gd
florian 31-Jul-2011 02: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 06: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 12: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 05: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
elassoto at hotmail dot com 10-Sep-2010 01:54
If you have already compiled PHP and want to recompile '--with-gd', don't forget to run 'make clean' first!
msc at homofaber dot com 30-Oct-2008 07: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