centos7.8环境下php7.2.24添加webp图片扩展的支持
公司线上的图片转换项目对webp格式的图片不支持转换,需要添加对webp格式的支持
经过搜索和测试发现两条可以实现的方式:
1.安装 imagick 扩展,让imagick支持webp的格式转换,必须是7.0以上版本
2.编译gd模块添加webp的格式支持,经过测试需要重新编译php(编译的时候不要把--with-gd参数带上,否则无法实现目标),然后把gd当成类似 xml,redis类似的插件进行加载
因为线上代码是使用gd的webp功能,需要重新编译php(系统功能简单,验证不费劲,且有多台机器可以使用,临时下掉一台重新编译问题不大)
现有的php支持的图片格式
# php --ri gd gd GD Support => enabled GD headers Version => 2.1.1 GD library Version => 2.1.1 FreeType Support => enabled FreeType Linkage => with freetype FreeType Version => 2.5.4 GIF Read Support => enabled GIF Create Support => enabled JPEG Support => enabled libJPEG Version => unknown PNG Support => enabled libPNG Version => 1.6.10 WBMP Support => enabled XPM Support => enabled libXpm Version => 30411 XBM Support => enabled Directive => Local Value => Master Value gd.jpeg_ignore_warning => 1 => 1
# 编译 php-7.2.24 参数
./configure --prefix=/usr/local/php-7.2.24_fpm --with-config-file-path=/usr/local/php-7.2.24_fpm/etc --with-openssl=/usr/local/lab/openssl-1.0.2p --with-libxml-dir=/usr --with-zlib-dir=/usr/local/lab/zlib-1.2.11 --with-bz2 --enable-calendar --with-curl=/usr/local/lab/curl-7.36.0 --enable-dba --enable-exif --enable-ftp --with-jpeg-dir=/usr/local/lab/jpeg-9b --with-png-dir=/usr/local/lab/libpng-1.6.10/ --with-freetype-dir=/usr/local/lab/freetype-2.5.4 --with-gd=/usr/local/lab/libgd-2.1.1 --with-gettext --enable-mbstring --with-ldap=/usr/local/openldap-2.4.23 --with-mcrypt=/usr/local/lab/libmcrypt-2.5.8_php7.2 --with-mhash=/usr/local/lab/mhash-0.9.9.9 --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-unixODBC=/usr/local/lab/unixODBC-2.3.2 --with-pdo-dblib=/usr/local/lab/freetds-0.92 --enable-zip --with-iconv-dir=/usr/local/lab/libiconv-1.14 --with-fpm-user=apache --with-fpm-group=users --enable-fpm --with-xmlrpc --enable-soap --enable-mbregex --enable-opcache --enable-inline-optimization --enable-xml --enable-sockets --disable-debug --enable-pcntl --enable-shmop
去掉了图片的编译(如果编译php时就加入这些参数,在单独编译gd时这些图片格式的支持就不会生效)
去掉了图片的参数
--with-freetype-dir=/usr/local/lab/freetype-2.5.4 --with-jpeg-dir=/usr/local/lab/jpeg-9b --with-png-dir=/usr/local/lab/libpng-1.6.10/
去掉了gd的参数
--with-gd=/usr/local/lab/libgd-2.1.1
1.重新编译PHP7.2.24
# 具体参数
./configure --prefix=/usr/local/php-7.2.24_withgd --with-config-file-path=/usr/local/php-7.2.24_withgd/etc --with-openssl=/usr/local/lab/openssl-1.0.2p --with-libxml-dir=/usr --with-zlib-dir=/usr/local/lab/zlib-1.2.11 --with-bz2 --enable-calendar --with-curl=/usr/local/lab/curl-7.36.0 --enable-dba --enable-exif --enable-ftp --with-gettext --enable-mbstring --with-ldap=/usr/local/openldap-2.4.23 --with-mcrypt=/usr/local/lab/libmcrypt-2.5.8_php7.2 --with-mhash=/usr/local/lab/mhash-0.9.9.9 --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-unixODBC=/usr/local/lab/unixODBC-2.3.2 --with-pdo-dblib=/usr/local/lab/freetds-0.92 --enable-zip --with-iconv-dir=/usr/local/lab/libiconv-1.14 --with-fpm-user=apache --with-fpm-group=users --enable-fpm --with-xmlrpc --enable-soap --enable-mbregex --enable-opcache --enable-inline-optimization --enable-xml --enable-sockets --disable-debug --enable-pcntl --enable-shmop
注意这两个路径,避免覆盖老版本的php
--prefix=/usr/local/php-7.2.24_withgd --with-config-file-path=/usr/local/php-7.2.24_withgd/etc
# -j 4 加速编译
make -j 4 && make install
2.单独编译gd 作为一个so模块进行加载
yum -y install libwebp-devel cd /usr/local/src/php-7.2.24/ext/gd /usr/local/php-7.2.24_withgd/bin/phpize
# 为了保障兼容性,添加了之前的编译图片插件的路径
./configure --with-php-config=/usr/local/php-7.2.24_withgd/bin/php-config --with-webp-dir --with-freetype-dir=/usr/local/lab/freetype-2.5.4 --with-jpeg-dir=/usr/local/lab/jpeg-9b --with-png-dir=/usr/local/lab/libpng-1.6.10/ --with-xpm-dir make -j 4&& make install
修改php.ini 添加
extension=gd.so
3.最终的结果
# /usr/local/php-7.2.24_withgd/bin/php --ri gd gd GD Support => enabled GD headers Version => 2.1.1 GD library Version => 2.1.1 FreeType Support => enabled FreeType Linkage => with freetype FreeType Version => 2.5.4 GIF Read Support => enabled GIF Create Support => enabled JPEG Support => enabled libJPEG Version => unknown PNG Support => enabled libPNG Version => 1.6.10 WBMP Support => enabled XPM Support => enabled libXpm Version => 30411 XBM Support => enabled Directive => Local Value => Master Value gd.jpeg_ignore_warning => 1 => 1
报错的处理
1.编译gd库报错处理:
[/usr/local/src/php-7.2.24/ext/gd]# make /bin/sh /usr/local/src/php-7.2.24/ext/gd/libtool --mode=compile cc -I/usr/local/src/php-7.2.24/ext/gd/libgd -DHAVE_LIBPNG -DHAVE_LIBWEBP -I. -I/usr/local/src/php-7.2.24/ext/gd -DPHP_ATOM_INC -I/usr/local/src/php-7.2.24/ext/gd/include -I/usr/local/src/php-7.2.24/ext/gd/main -I/usr/local/src/php-7.2.24/ext/gd -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /usr/local/src/php-7.2.24/ext/gd/gd.c -o gd.lo mkdir .libs cc -I/usr/local/src/php-7.2.24/ext/gd/libgd -DHAVE_LIBPNG -DHAVE_LIBWEBP -I. -I/usr/local/src/php-7.2.24/ext/gd -DPHP_ATOM_INC -I/usr/local/src/php-7.2.24/ext/gd/include -I/usr/local/src/php-7.2.24/ext/gd/main -I/usr/local/src/php-7.2.24/ext/gd -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /usr/local/src/php-7.2.24/ext/gd/gd.c -fPIC -DPIC -o .libs/gd.o /usr/local/src/php-7.2.24/ext/gd/gd.c:75:24: fatal error: ft2build.h: No such file or directory # include <ft2build.h> ^ compilation terminated. make: *** [gd.lo] Error 1
解决办法:
export C_INCLUDE_PATH=/usr/local/lab/freetype-2.5.4/include/freetype2 export CPLUS_INCLUDE_PATH=/usr/local/lab/freetype-2.5.4/include/freetype2
2.编译php的错误处理 /usr/bin/ld: cannot find -lpng12
.lo sapi/cli/ps_title.lo sapi/cli/php_cli_process_title.lo -lcrypt -lcrypto -lssl -lcrypto -lz -lresolv -lcrypt -lrt -lsybdb -lldap -llber -lpng -lz -ljpeg -lcrypto -lssl -lcrypto -lbz2 -lz -lcrypto -lssl -lcrypto -lrt -lm -ldl -lnsl -lxml2 -lz -lm -ldl -lcurl -lxml2 -lz -lm -ldl -lfreetype -lodbc -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lcrypt -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lcrypt -o sapi/cli/php /usr/bin/ld: cannot find -lpng12 collect2: error: ld returned 1 exit status make: *** [sapi/cli/php] Error 1
处理办法:
ln -s /usr/local/lab/libiconv-1.14/lib/libiconv.so.2 /usr/lib64/
# 关键
yum install -y libpng12-devel
编译一台以后就可以直接 拷贝 /usr/local/php-7.2.24_withgd 这个目录到别的机器直接运行了,同时需要拷贝 /usr/local/src/php-7.2.24源码安装文件到目标机器
# -j 4 加速编译