Zend_Captcha生成验证码实例
时间:2008-10-17
来源:互联网
小弟不才 学习PHP还没多久 技术谈不上
发个简单的代码 请大家批评
原文:http://hi.baidu.com/haomao/blog/item/ea0aaf4b0b2f93f682025ce8.html
效果图: [躁点为10]
========================
首先是CONTROLLER里的代码
//生成随即验证码
$this->codeSession = new Zend_Session_Namespace('code'); //在默认构造函数里实例化
$captcha = new Zend_Captcha_Image(array('font'=>'./public/images/faktos.ttf', //字体文件路径
'fontsize'=>24, //字号
'imgdir'=>'./public/images/code/', //验证码图片存放位置
'session'=>$this->codeSession, //验证码session值
'width'=>120, //图片宽
'height'=>50, //图片高
'wordlen'=>5 )); //字母数
$captcha->setGcFreq(3); //设置删除生成的旧的验证码图片的随机几率
$captcha->generate(); //生成图片
$this->view->ImgDir = $captcha->getImgDir();
$this->view->captchaId = $captcha->getId(); //获取文件名,md5编码
$this->codeSession->code=$captcha->getWord(); //获取当前生成的验证字符串
echo $this->codeSession->code;
----------------------------------------------
view页面调用代码
<span id="captcha"><img src="<?php echo $this->ImgDir,$this->captchaId ?>.png" border="0" />
值得注意的是。
系统默认的验证图片躁点较多
如果需要修改躁点个数
编辑 Zend/Captcha/Image.php
393行 ,454行 两个地方同时改动红色数字 降低躁点
// generate noise
for ($i=0; $i<100; $i++) {
imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
}
最让人想不通的是 Zend居然每刷新一次 就生成一张图片 并且存放到 $imgdir 下面
网上有朋友已经修改过源码 采用header方式.
在这里可以设定一个图片数量 超过限制就自动删除。 如上代码所示 $captcha->setGcFreq(3)
另外需要修改 protected function _gc() 函数为:
protected function _gc()
{
//$expire = time() - $this->getExpiration();
$expire = time();
echo $this->getExpiration();
foreach (new DirectoryIterator($this->getImgDir()) as $file) {
if (!$file->isDot() && !$file->isDir()) {
if ($file->getMTime() < $expire) {
unlink($file->getPathname());
}
}
}
}
=========================
确实很简单
谈不上技术 大家被笑话
发个简单的代码 请大家批评
原文:http://hi.baidu.com/haomao/blog/item/ea0aaf4b0b2f93f682025ce8.html
效果图: [躁点为10]
========================
首先是CONTROLLER里的代码
//生成随即验证码
$this->codeSession = new Zend_Session_Namespace('code'); //在默认构造函数里实例化
$captcha = new Zend_Captcha_Image(array('font'=>'./public/images/faktos.ttf', //字体文件路径
'fontsize'=>24, //字号
'imgdir'=>'./public/images/code/', //验证码图片存放位置
'session'=>$this->codeSession, //验证码session值
'width'=>120, //图片宽
'height'=>50, //图片高
'wordlen'=>5 )); //字母数
$captcha->setGcFreq(3); //设置删除生成的旧的验证码图片的随机几率
$captcha->generate(); //生成图片
$this->view->ImgDir = $captcha->getImgDir();
$this->view->captchaId = $captcha->getId(); //获取文件名,md5编码
$this->codeSession->code=$captcha->getWord(); //获取当前生成的验证字符串
echo $this->codeSession->code;
----------------------------------------------
view页面调用代码
<span id="captcha"><img src="<?php echo $this->ImgDir,$this->captchaId ?>.png" border="0" />
值得注意的是。
系统默认的验证图片躁点较多
如果需要修改躁点个数
编辑 Zend/Captcha/Image.php
393行 ,454行 两个地方同时改动红色数字 降低躁点
// generate noise
for ($i=0; $i<100; $i++) {
imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
}
最让人想不通的是 Zend居然每刷新一次 就生成一张图片 并且存放到 $imgdir 下面

网上有朋友已经修改过源码 采用header方式.
在这里可以设定一个图片数量 超过限制就自动删除。 如上代码所示 $captcha->setGcFreq(3)
另外需要修改 protected function _gc() 函数为:
protected function _gc()
{
//$expire = time() - $this->getExpiration();
$expire = time();
echo $this->getExpiration();
foreach (new DirectoryIterator($this->getImgDir()) as $file) {
if (!$file->isDot() && !$file->isDir()) {
if ($file->getMTime() < $expire) {
unlink($file->getPathname());
}
}
}
}
=========================
确实很简单

作者: sheak 发布时间: 2008-10-17
不错啊,学习了先
作者: 西门一笑 发布时间: 2008-11-10
不错,收藏先1
作者: 0hudu 发布时间: 2008-11-10
只需要这个Image.php一个文件包含进来就可以了吗?
作者: e_zailai 发布时间: 2008-11-10
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28