Zend验证码图像生成空白


Zend captcha Image generates blank

图像验证码随机显示空白验证码。大概10次中有4次。

我已经发现了这个问题,zend论坛上的一个帖子建议唯一的修复方法是编辑zend框架代码。

有没有办法解决这个问题,而不编辑ZF代码?为什么Zend从那时起就没有在发布中修复这个问题,我看了一下更改日志,但没有与此相关的内容?

$w     = $this->getWidth();
$h     = $this->getHeight();
$fsize = $this->getFontSize();
$img_file   = $this->getImgDir() . $id . $this->getSuffix();
if(empty($this->_startImage)) {
    $img = imagecreatetruecolor($w, $h);
} else {
    $img = imagecreatefrompng($this->_startImage);
    if(!$img) {
        require_once 'Zend/Captcha/Exception.php';
        throw new Zend_Captcha_Exception("Can not load start image");
    }
    $w = imagesx($img);
    $h = imagesy($img);
}
$text_color = imagecolorallocate($img, 0, 0, 0);
$bg_color   = imagecolorallocate($img, 255, 255, 255);
imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color);
$textbox = imageftbbox($fsize, 0, $font, $word);
$x = ($w - ($textbox[2] - $textbox[0])) / 2;
$y = ($h - ($textbox[7] - $textbox[1])) / 2;
// Edit: check not over 100
if($x > 50){ $x = 50;}
if($y > 100){ $y = 100;}
imagefttext($img, $fsize, 0, $x, $y, $text_color, $font, $word);

使用die()进行调试证明是这种情况:

die("img=".$img ." fsize=".$fsize. " x=".$x ." y=".$y . " h=".$h );
img=Resource id #159 fsize=24 x=1073741923.5 y=41 h=50

根据提供的链接,这可能不是Zend Framework的错误,而是imageftbbox的错误。我建议扩展Zend_Captcha_Image并复制_generateImage函数并将$x =设置为合理的东西(我在论坛帖子上看到45),当它太大(可能> 100)。这允许您在不更改实际Zend框架代码的情况下放入建议的修复/hack。

您是否碰巧在Debian/Ubuntu上运行php 5.3.2 ?PHP(不是Zend)中有一个imageftbbox错误,会导致它偶尔返回异常值。

我认为这与使用32位gcc编译特定的GD版本有关。我在验证码图片上看到过很多次这样的情况。简单的修复方法是将PHP升级到最新版本,或者我认为它已经在下一个PHP版本中修复了。