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 相关文章推荐
实现 win2003 下 mysql 数据库每天自动备份
Dec 06 PHP
discuz的php防止sql注入函数
Jan 17 PHP
PHP笔记之:基于面向对象设计的详解
May 14 PHP
PHP随机生成唯一HASH值自定义函数
Apr 20 PHP
yii2 页面底部加载css和js的技巧
Apr 21 PHP
php基于websocket搭建简易聊天室实践
Oct 24 PHP
thinkPHP模板中for循环与switch语句用法示例
Nov 30 PHP
IOS 开发之NSDictionary转换成JSON字符串
Aug 14 PHP
PHP实现的字符串匹配算法示例【sunday算法】
Dec 19 PHP
PHP实现Huffman编码/解码的示例代码
Apr 20 PHP
PHP面向对象类型约束用法分析
Jun 12 PHP
PHP设计模式之 策略模式Strategy详解【对象行为型】
May 01 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里的中文变量说明
2011/07/23 PHP
深入extjs与php参数交互的详解
2013/06/25 PHP
PHP中Restful api 错误提示返回值实现思路
2016/04/12 PHP
thinkPHP和onethink微信支付插件分享
2019/08/11 PHP
Ruffy javascript 学习笔记
2009/11/30 Javascript
两个JavaScript jsFiddle JSBin在线调试器
2010/03/14 Javascript
一个原生的用户等级的进度条
2010/07/03 Javascript
jquery插件jquery倒计时插件分享
2013/12/27 Javascript
javascript对话框使用方法(警告框 javascript确认框 提示框)
2014/01/07 Javascript
jquery实现倒计时效果
2015/12/14 Javascript
Javascript typeof与instanceof的区别
2016/10/18 Javascript
Angular2学习笔记——详解NgModule模块
2016/12/02 Javascript
JS使用正则表达式验证身份证号码
2017/06/23 Javascript
node.js利用mongoose获取mongodb数据的格式化问题详解
2017/10/06 Javascript
微信小程序授权登录及解密unionId出错的方法
2018/09/26 Javascript
JS实现获取自定义属性data值的方法示例
2018/12/19 Javascript
js get和post请求实现代码解析
2020/02/06 Javascript
vue+axios全局添加请求头和参数操作
2020/07/24 Javascript
vue-以文件流-blob-的形式-下载-导出文件操作
2020/08/07 Javascript
详解 javascript对象创建模式
2020/10/30 Javascript
Windows和Linux下使用Python访问SqlServer的方法介绍
2015/03/10 Python
Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法
2017/01/12 Python
Python SQLite3简介
2018/02/22 Python
Python设计模式之外观模式实例详解
2019/01/17 Python
OpenCV HSV颜色识别及HSV基本颜色分量范围
2019/03/22 Python
python合并多个excel文件的示例
2020/09/23 Python
使用py-spy解决scrapy卡死的问题方法
2020/09/29 Python
CSS3中:nth-child和:nth-of-type的区别深入理解
2014/03/10 HTML / CSS
Lacoste美国官网:经典POLO衫品牌
2016/10/12 全球购物
得到Class的三个过程是什么
2012/08/10 面试题
集团公司总经理岗位职责
2013/12/20 职场文书
遗体告别仪式主持词
2014/03/20 职场文书
服务承诺书范文
2014/05/19 职场文书
买房协议书范本
2014/10/23 职场文书
限期整改通知书
2015/04/22 职场文书
2015年节能降耗工作总结
2015/05/22 职场文书