最近想重新编译一下php的gd扩展,执行了如下一串命令:
cd /opt/software/php-7.4.8/ext/gd make clean phpize ./configure --with-php-config=/usr/bin/php-config --with-jpeg-dir --with-png-dir --with-freetype-dir --with-zlib-dir --with-gd
结果就报 syntax error near unexpected token ZLIB的错误,具体如下所示:
checking for gawk... gawk checking for GD support... yes, shared checking for external libgd... no checking for libwebp... no checking for libjpeg... no checking for libXpm... no checking for FreeType 2... no checking whether to enable JIS-mapped Japanese font support in GD... no ./configure: line 4124: syntax error near unexpected token `ZLIB,' ./configure: line 4124: ` PKG_CHECK_MODULES(ZLIB, zlib)'
服务器安装了两个版本的php,编译进入的是php7.4版本的源码包,但上面phpize命令默认对应的是php7.2版本,后面php-config的路径也是如此
cd /opt/software/php-7.4.8/ext/gd
make clean
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-freetype --enable-gd
make && make install
上一次重新编译gd扩展的经历,是在公司的服务器上。
因为程序报call to undefined function imagecreatefromjpeg的错误,也就是没把对jpeg的支持编译进去。
关于那一次踩坑经历的详细,请见这篇文章:php装了gd扩展,还是报call to undefined function imagecreatefromjpeg错误
这一次是因为要给自己博客里图片添加水印,但是nginx错误日志里报Call to undefined function imagettfbbox()错误,导致水印无法添加成功。
原因就是因为gd扩展缺少了freetype的组件,百科里是这样介绍freetype的:
FreeType库是一个完全免费(开源)的、高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件,包括TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF等。
imagettfbbox函数中就有一个字体文件fontFile的参数,也就是这里识别字体要用到freetype吧。
所以我就需要重新编译gd扩展,把这个freetype加进去。
本文为翟码农个人博客蓝翟红尘里php分类下的有关编译安装gd踩坑的原创文章,转载请注明出处:http://www.zhai14.com/blog/syntax-error-near-unexpected-token-in-compilation-of-gd.html
./configure: line 4124: syntax error near unexpected token `ZLIB,' ./configure: line 4124: ` PKG_CHECK_MODULES(ZLIB, zlib)'
根据上面错误提示,网上有的从这一角度入手:
编译环境找不到 pkg-config 的宏PKG_CHECK_MODULES,很可能是因为在引导时 aclocal 无法找到 pkg.m4,这是因为未安装 pkg-config 或已将其安装在 aclocal 不知道的地方。
执行
whereis pkg-config
和
aclocal --print
这两条命令,确定我既安装了pkg-config,在aclocal路径下也能找到pkg.m4文件,所以问题不在这。
接着我检查是不是依赖没安装,然后确实发现freetype没安装,就立马给安装上了。
从这里可以下载安装包:https://download.savannah.gnu.org/releases/freetype/
解压后,就开始编译安装:
cd ./freetype2.8
./configure --prefix=/usr/local/freetype
make && make install
最终freetype装好了,还是报错。
意外发现php --version是7.2版本,而phpinfo里看到的是php7.4版本,两个版本号不一致,这才想到问题可能就出在这。
最终将编译命令调整如下,就编译成功了。
cd /opt/software/php-7.4.8/ext/gd make clean /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-freetype --enable-gd make && make install
编译成功后,就可以看到so文件所在目录:
Build complete. Don't forget to run 'make test'. Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20190902/ Installing header files: /usr/local/php/include/php/
由于我这次是二次编译,所以php.ini文件里第一次就已经引入so文件了。这次只需要重启php-fpm就好了。
ps -ef |grep php-fpm | grep -v "grep php-fpm" | cut -c 9-16 | xargs kill -9 /usr/bin/php-fpm -v (检查版本号是否与phpinfo一致) /usr/bin/php-fpm
最后检查下phpinfo里gd模块,出现了freetype,上传照片的水印也有了。
附录: