自己编译php_gd

extension=php_gd2.dll

找到php的配置文件php.ini,搜索extension=php_gd2.dll,去掉前面的分号即可;
如果没有直接添加这种情况适合于windows系统和编译时支持gd的php,保存后重启apache即可
如果用的是安装版本如Ubuntu的deb。redhat的rpm安装的php可以使用命令安装即可
    Ubuntu:sudo apt-get install php5-gd
    redhat:yum install php-gd
FreeBSD可以使用systeminstall的package安装gd2解决
如果是编译安装并且没有选择支持gd需要先安装gd,然后重新编译php

运行代码:

yum install php-gd

[root@VM-0-17-centos ~]# yum install php-gd
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package php-gd.x86_64 0:5.4.16-48.el7 will be installed
--> Processing Dependency: php-common(x86-64) = 5.4.16-48.el7 for package: php-gd-5.4.16-48.el7.x86_64
--> Processing Dependency: libt1.so.5()(64bit) for package: php-gd-5.4.16-48.el7.x86_64
--> Running transaction check
---> Package php-common.x86_64 0:5.4.16-48.el7 will be installed
--> Processing Dependency: libzip.so.2()(64bit) for package: php-common-5.4.16-48.el7.x86_64
---> Package t1lib.x86_64 0:5.1.2-14.el7 will be installed
--> Running transaction check
---> Package libzip.x86_64 0:0.10.1-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
Package Arch Version Repository Size
=======================================================================================
Installing:
php-gd x86_64 5.4.16-48.el7 os 128 k
Installing for dependencies:
libzip x86_64 0.10.1-8.el7 os 48 k
php-common x86_64 5.4.16-48.el7 os 565 k
t1lib x86_64 5.1.2-14.el7 os 166 k

Transaction Summary
=======================================================================================
Install 1 Package (+3 Dependent packages)

Total download size: 908 k
Installed size: 4.6 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): libzip-0.10.1-8.el7.x86_64.rpm | 48 kB 00:00:00
(2/4): php-gd-5.4.16-48.el7.x86_64.rpm | 128 kB 00:00:00
(3/4): php-common-5.4.16-48.el7.x86_64.rpm | 565 kB 00:00:00
(4/4): t1lib-5.1.2-14.el7.x86_64.rpm | 166 kB 00:00:00
---------------------------------------------------------------------------------------
Total 1.6 MB/s | 908 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libzip-0.10.1-8.el7.x86_64 1/4
Installing : php-common-5.4.16-48.el7.x86_64 2/4
Installing : t1lib-5.1.2-14.el7.x86_64 3/4
Installing : php-gd-5.4.16-48.el7.x86_64 4/4
Verifying : t1lib-5.1.2-14.el7.x86_64 1/4
Verifying : php-gd-5.4.16-48.el7.x86_64 2/4
Verifying : libzip-0.10.1-8.el7.x86_64 3/4
Verifying : php-common-5.4.16-48.el7.x86_64 4/4

Installed:
php-gd.x86_64 0:5.4.16-48.el7

Dependency Installed:
libzip.x86_64 0:0.10.1-8.el7 php-common.x86_64 0:5.4.16-48.el7
t1lib.x86_64 0:5.1.2-14.el7

Complete!

//-----------------------------------------------------------------------


看过我的这篇编译LNMP文章的同学应该知道,我的php是编译安装的,并且gd库是采用静态编译的。下面是当时编译php的参数。

./configure --prefix=/usr/local/php \
--with-gd \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--enable-mysqlnd \
--with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--enable-mbstring \
--enable-fpm

但是今天就出现了一个问题,我的gd库中没有libjpeg支持,看看错误提示,不能使用imagecreatefromjpeg方法了。

Fatal error: Call to undefined function imagecreatefromjpeg()
1
解决方法
当然要重新编译gd啦,但是又由于我采用的是静态编译,所以必须要重新编译php,然后编译gd,最后在php.ini文件中添加扩展so文件。

1、重新编译php
重新编译php,不要静态编译gd库,也就是说,不要使用--with-gd这样的参数了,使用下面的编译格式。

./configure --prefix=/usr/local/php \
--enable-mysqlnd \
--with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--enable-mbstring \
--enable-fpm

make && make install

2、编译libjpeg v9b
cd /usr/local/src
wget http://www.ijg.org/files/jpegsrc.v9b.tar.gz
tar xzvf jpegsrc.v9b.tar.gz
./configure --prefix=/usr/local/libjpeg --enable-shared --enable-static
make && make install

3、重新编译gd扩展,给gd扩展添加上libjpeg
cd /usr/local/src/php-5.6.30/ext/gd #gd的源码目录
/usr/local/php/bin/phpize #生成configure命令

./configure --with-php-config=/usr/local/php/bin/php-config -with-png-dir --with-freetype-dir --with-jpeg-dir=/usr/local/libjpeg -with-zlib-dir --with-gd

make && make install#编译gd

此时会提示:Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226(这个路径就是gd扩展的路径)

4、添加gd扩展
cd /usr/local/php
vim lib/php.ini

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/gd.so #添加新行

5、重启服务器
我的是Nginx,所以只需要重新启动php-fpm就行了,看看是否有jpeg支持了。


————————————————
版权声明:本文为CSDN博主「麦超」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/baochao95/article/details/72578300

原文地址:https://www.cnblogs.com/chenjian/p/15539097.html