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
相關文章
相關標籤/搜索