PHP 动态随机生成验证码类代码


Posted in PHP onApril 09, 2010

下面是效果图,这个效果图是没有开启干扰码的效果图
PHP 动态随机生成验证码类代码
下面是类代码

<?php 
/************************************************ 
//FILE:ImageCode 
//DONE:生成动态验证码类 
//DATE"2010-3-31 
//Author:www.5dkx.com 5D开心博客 
************************************************************************/ 
class ImageCode{ 
private $width; //验证码图片宽度 
private $height; //验证码图片高度 
private $codeNum; //验证码字符个数 
private $checkCode; //验证码字符 
private $image; //验证码画布 
/************************************************************************ 
// Function:构造函数 
// Done:成员属性初始化 
// Author:www.5dkx.com 5D开心博客 
************************************************************************/ 
function __construct($width=60,$height=20,$codeNum=4) 
{ 
$this->width = $width; 
$this->height = $height; 
$this->codeNum = $codeNum; 
$this->checkCode = $this->createCheckCode(); 
} 
function showImage() 
{ 
$this->getcreateImage(); 
$this->outputText(); 
$this->setDisturbColor(); 
$this->outputImage(); 
} 
function getCheckCode() 
{ 
return $this->chekCode; 
} 
private function getCreateImage() 
{ 
$this->image = imagecreatetruecolor($this->width,$this->height); 
$back = imagecolorallocate($this->image,255,255,255); 
$border = imagecolorallocate($this->image,255,255,255); 
imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border); 
//使用纯白色填充矩形框,这里用的话后面干扰码失效 
/*如果想用干扰码的话使用下面的*/ 
//imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border); 
} 
private function createCheckCode() 
{ 
for($i=0;$i<$this->codeNum;$i++) 
{ 
$number = rand(0,2); 
switch($number) 
{ 
case 0: $rand_number = rand(48,57); break;//数字 
case 1: $rand_number = rand(65,90);break;//大写字母 
case 2: $rand_number = rand(97,122);break;//小写字母 
} 
$asc = sprintf("%c",$rand_number); 
$asc_number = $asc_number.$asc; 
} 
return $asc_number; 
} 
private function setDisturbColor()//干扰吗设置 
{ 
for($i=0;$i<=100;$i++) 
{ 
//$color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255)); 
$color = imagecolorallocate($this->image,255,255,255); 
imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color); 
} 
//$color = imagecolorallocate($this->image,0,0,0); 
//imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color); 
} 
private function outputText() 
{ 
//随机颜色、随机摆放、随机字符串向图像输出 
for($i=0;$i<=$this->codeNum;$i++) 
{ 
$bg_color = imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255)); 
$x = floor($this->width/$this->codeNum)*$i+3; 
$y = rand(0,$this->height-15); 
imagechar($this->image,5,$x,$y,$this->checkCode[$i],$bg_color); 
} 
} 
private function outputImage() 
{ 
if(imagetypes()&IMG_GIF) 
{ 
header("Content_type:image/gif"); 
imagegif($this->image); 
} 
elseif(imagetypes()&IMG_JPG) 
{ 
header("Content-type:image/jpeg"); 
imagejpeg($this->image,"",0.5); 
} 
elseif(imagetypes()&IMG_PNG) 
{ 
header("Content-type:image/png"); 
imagejpeg($this->image); 
} 
elseif(imagetypes()&IMG_WBMP) 
{ 
header("Content-type:image/vnd.wap.wbmp"); 
imagejpeg($this->image); 
} 
else 
{ 
die("PHP不支持图像创建"); 
} 
} 
function __destruct() 
{ 
imagedestroy($this->image); 
} 
} 
/*显示*/ 
/******************************************************************* 
session_start(); 
$image = new ImageCode(60,20,4); 
$image->showImage(); 
$_SESSION['ImageCode'] = $image->getCheckCode(); 
*******************************************************************/ 
?>
PHP 相关文章推荐
PHP网上调查系统
Oct 09 PHP
php echo 输出字符串函数详解
May 13 PHP
基于PHP一些十分严重的缺陷详解
Jun 03 PHP
Windows下的PHP安装文件线程安全和非线程安全的区别
Apr 23 PHP
PHP将HTML转换成文本的实现代码
Jan 21 PHP
Java和PHP在Web开发方面对比分析
Mar 01 PHP
wordpress安装过程中遇到中文乱码的处理方法
Apr 21 PHP
教你php如何实现验证码
Jan 20 PHP
php+jQuery实现的三级导航栏下拉菜单显示效果
Aug 10 PHP
PHP实现求两个字符串最长公共子串的方法示例
Nov 17 PHP
PHP的mysqli_select_db()函数讲解
Jan 23 PHP
TP3.2.3框架文件上传操作实例详解
Jan 23 PHP
DedeCMS 核心类TypeLink.class.php摘要笔记
Apr 07 #PHP
通俗易懂的php防注入代码
Apr 07 #PHP
Ext.data.PagingMemoryProxy分页一次性读取数据的实现代码
Apr 07 #PHP
用PHP实现读取和编写XML DOM代码
Apr 07 #PHP
php session和cookie使用说明
Apr 07 #PHP
DedeCMS dede_channeltype表字段注释
Apr 07 #PHP
php抓取https的内容的代码
Apr 06 #PHP
You might like
深入PHP autoload机制的详解
2013/06/09 PHP
在Win7 中为php扩展配置Xcache
2014/10/08 PHP
PHP缓冲区用法总结
2016/02/14 PHP
PHP实现读取文件夹及批量重命名文件操作示例
2019/04/15 PHP
php使用socket调用http和smtp协议实例小结
2019/07/26 PHP
Yii框架 session 数据库存储操作方法示例
2019/11/18 PHP
javascript 流畅动画实现原理
2009/09/08 Javascript
让低版本浏览器支持input的placeholder属性(js方法)
2013/04/03 Javascript
js写的评论分页(还不错)
2013/12/23 Javascript
js获取视频时长代码
2014/04/10 Javascript
JS实现支持多选的遍历下拉列表代码
2015/08/20 Javascript
JavaScript类型系统之布尔Boolean类型详解
2016/06/26 Javascript
基于jQuery制作小图标上下滑动特效
2017/01/18 Javascript
利用原生JS与jQuery实现数字线性变化的动画
2017/02/24 Javascript
js实现登录框鼠标拖拽效果
2017/03/09 Javascript
微信小程序分享海报生成的实现方法
2018/12/10 Javascript
详解react阻止无效重渲染的多种方式
2018/12/11 Javascript
vue-cli3中配置alias和打包加hash值操作
2020/09/04 Javascript
python切换hosts文件代码示例
2013/12/31 Python
python自动化测试之从命令行运行测试用例with verbosity
2014/09/28 Python
用Python编写一个简单的Lisp解释器的教程
2015/04/03 Python
python设计模式大全
2016/06/27 Python
Python中常用信号signal类型实例
2018/01/25 Python
python实现简单http服务器功能
2018/09/17 Python
如何爬取通过ajax加载数据的网站
2019/08/15 Python
解决启动django,浏览器显示“服务器拒绝访问”的问题
2020/05/13 Python
CSS3 选择器 伪类选择器介绍
2012/01/21 HTML / CSS
css3的过滤效果简单实例
2016/08/03 HTML / CSS
幼儿园保育员辞职信
2014/01/12 职场文书
淘宝中秋节活动方案
2014/01/31 职场文书
事业单位竞聘上岗实施方案
2014/03/28 职场文书
主题实践活动总结
2014/05/08 职场文书
年度考核登记表个人总结
2015/03/06 职场文书
2015年银行客户经理工作总结
2015/04/01 职场文书
2016年小学生寒假总结
2015/10/10 职场文书
《勇者辞职不干了》ED主题曲无字幕动画MV公开
2022/04/13 日漫