今天为ETS网站增加了注册时需要验证码的功能.在本地测试正常,上传到线上之后发现无法正常显示验证码图片 检查发现验证码刷新的动作请求503
了
这个功能我再其他网站也添加了都没有问题。本地也没问题,那么最大可能就是线上环境导致的问题。 于是检查系统错误日志,发现在日志中有:Image CAPTCHA requires FT fonts support
错误。
于是google
搜索了一下,有人说是GD
扩展没有安装造成的。
但我查看线上phpinfo()
发现GD
扩展有安装。
我只好在代码中搜索报错提示Image CAPTCHA requires FT fonts support
查找到报错代码位置 lib\Zend\Captcha\Image.php
460行:
protected function _generateImage($id, $word)
{
if (!extension_loaded("gd")) {
#require_once 'Zend/Captcha/Exception.php';
throw new Zend_Captcha_Exception("Image CAPTCHA requires GD extension");
}
if (!function_exists("imagepng")) {
#require_once 'Zend/Captcha/Exception.php';
throw new Zend_Captcha_Exception("Image CAPTCHA requires PNG support");
}
if (!function_exists("imageftbbox")) {
#require_once 'Zend/Captcha/Exception.php';
throw new Zend_Captcha_Exception("Image CAPTCHA requires FT fonts support");
}
.........
可查看到代码报错条件是找不到 imageftbbox
方法。 在php
官网查看imageftbbox
方法在注释中发现了重点:
Note: 此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。
Note: 此函数仅在 PHP 编译时加入 freetype 支持时有效(--with-freetype-dir=DIR )。
这个函数需要GD
扩展版本不低于2.0.1
并且需要编译加入freetype
!!!
我想这应该就是问题所在了,我的GD
扩展版本虽然大于2.0.1
但 没有支持freetype
.在phpinfo()
的GD
扩展显示中并没有freetype
于是网上搜索教程安装freetype
,并更新GD
扩展
安装freetype
- 下载freetype-2.4.0.tar.gz
- 解压 freetype-2.4.0.tar.gz:
tar -zxvf freetype-2.4.0.tar.gz
- 编译安装
freetype
cd freetype-2.4.0 ./configure --prefix=/usr/local/freetype --enable-shared make && make install
也可直接用yum install freetype 直接安装
重新编译安装GD
扩展
- 进入php源码安装目录的扩展文件夹
cd /usr/src/php-5.5.29/ext/gd
- 执行make清除原有安装记录
make clean
/usr/local/php/bin/phpize
- 重写编译配置
./configure --with-php-config=/server/programs/php/bin/php-config --with-zlib-dir --with-png-dir --with-freetype-dir --with-jpeg-dir --with-gd
make && make install
开启扩展
vi /usr/local/php/lib/php.ini
- 添加
extension=gd.so
重启服务
/etc/init.d/nginx restart
/etc/init.d/php-fpm restart
问题来了
我的php
环境,最开始编译安装时已启用了GD
扩展,但不支持freetype
,现在希望在不重新安装php
的情况下,使其支持freetype
。
我尝试用phpize
重新编译GD
库扩展生成gd.so
但在php.ini
中加入extension=gd.so
后重启php
,会报错PHP Warning: Module 'gd' already loaded in Unknown on line 0
。
所以在php.ini
中添加启用GD
扩展是不行的.
原因是编译安装php
时启用的扩展,属于静态扩展,如需改动,只能重新编译安装php
但安装完成php后,通过phpize安装的动态扩展,是可以通过重新编译安装进行改动,不需要重新编译安装php
所以我这种情况还是只能重新编译PHP
重新编译PHP
cd /usr/src/php-5.5.29
make clean
- 添加上freetype配置参数
./configure --prefix=/usr/local/php --enable-fpm --with-zlib --enable-mbstring --with-openssl --with-mysql --with-mysqli --with-mysql-sock --with-gd --with-jpeg-dir=/usr/lib --enable-gd-native-ttf --enable-pdo --with-gettext --with-curl --with-pdo-mysql --enable-sockets --enable-bcmath --enable-xml --with-bz2 --enable-zip --with-jpeg-dir=/usr/local/jpeg --with-png-dir=/usr/local/png --with-mcrypt=/usr/local/libmcrypt --with-freetype-dir=/usr/local/freetype
make && make install
/etc/init.d/php-fpm restart
最后查看phpinfo()
信息的GD
扩展,检查是否有freetype
库
FreeType Support | enabled |
---|---|
FreeType Linkage | with freetype |
FreeType Version | 2.4.0 |