Magento 添加验证码,请求503,系统日志报错:Image CAPTCHA requires FT fonts support

今天为ETS网站增长了注册时须要验证码的功能.在本地测试正常,上传到线上以后发现没法正常显示验证码图片 检查发现验证码刷新的动做请求503php

这个功能我再其余网站也添加了都没有问题。本地也没问题,那么最大可能就是线上环境致使的问题。 因而检查系统错误日志,发如今日志中有:Image CAPTCHA requires FT fonts support 错误。mysql

因而google 搜索了一下,有人说是GD扩展没有安装形成的。nginx

但我查看线上phpinfo()发现GD扩展有安装。sql

我只好在代码中搜索报错提示Image CAPTCHA requires FT fonts supportcurl

查找到报错代码位置 lib\Zend\Captcha\Image.php 460行:socket

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!!!php-fpm

我想这应该就是问题所在了,个人GD扩展版本虽然大于2.0.1 但 没有支持freetype.在phpinfo()GD扩展显示中并无freetype 因而网上搜索教程安装freetype,并更新GD扩展测试

安装freetype

  1. 下载freetype-2.4.0.tar.gz
  2. 解压 freetype-2.4.0.tar.gz:
    tar -zxvf freetype-2.4.0.tar.gz
  3. 编译安装freetype
    cd freetype-2.4.0
    ./configure --prefix=/usr/local/freetype --enable-shared
    make && make install

也可直接用yum install freetype 直接安装网站

从新编译安装GD扩展

  1. 进入php源码安装目录的扩展文件夹
    cd /usr/src/php-5.5.29/ext/gd
  2. 执行make清除原有安装记录
    make clean
  3. /usr/local/php/bin/phpize
  4. 重写编译配置
    ./configure --with-php-config=/server/programs/php/bin/php-config --with-zlib-dir --with-png-dir --with-freetype-dir --with-jpeg-dir --with-gd
  5. 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

  1. cd /usr/src/php-5.5.29
  2. make clean
  3. 添加上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
  1. make && make install
  2. /etc/init.d/php-fpm restart

最后查看phpinfo()信息的GD扩展,检查是否有freetype

FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.4.0
相关文章
相关标签/搜索