打开PHP的GD功能
php.ini中修改
extension=php_gd2.dll
Controller代码
public function imgAction(){
Zend_Loader::loadClass("Zend_Session_Namespace");
Zend_Loader::loadClass("Zend_Captcha_Image");
$this->codeSession = new Zend_Session_Namespace('code'); //在默认构造函数里实例化
$captcha = new Zend_Captcha_Image(array('font'=>'./public/arialbd.ttf', //字体文件路径
'fontsize'=>24, //字号
'imgdir'=>'./public/images/code/', //验证码图片存放位置
'session'=>$this->codeSession, //验证码session值
'width'=>120, //图片宽
'height'=>50, //图片高
'wordlen'=>5 )); //字母数
$captcha->setLineNoiseLevel(3);//设置线干扰级别(默认5)
$captcha->setDotNoiseLevel(50);//设置点干扰级别(默认100)
$captcha->setGcFreq(10); //设置删除生成的旧的验证码图片的随机几率(默认10)
$captcha->generate(); //生成图片
$this->view->ImgDir = $captcha->getImgDir();
$this->view->captchaId = $captcha->getId(); //获取文件名,md5编码
$this->codeSession->code=$captcha->getWord(); //获取当前生成的验证字符串
$p = $this->view->ImgDir . $captcha->getId() . $captcha->getSuffix();
//$captcha
echo $this->codeSession->code;
echo "<img src='/$p' />" ;
//exit;
}
验证刚才的验证码
Zend_Loader::loadClass("Zend_Session_Namespace");
$this->codeSession = new Zend_Session_Namespace('code');
echo $this->codeSession->code;