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 相关文章推荐
完美解决dedecms中的[html][/html]和[code][/code]问题
Mar 20 PHP
set_include_path在win和linux下的区别
Jan 10 PHP
PHP循环获取GET和POST值的代码
Apr 09 PHP
PHP错误抑制符(@)导致引用传参失败Bug的分析
May 02 PHP
ThinkPHP基本的增删查改操作实例教程
Aug 22 PHP
thinkphp3.2点击刷新生成验证码
Feb 16 PHP
php自定义函数实现统计中文字符串长度的方法小结
Apr 15 PHP
PHP实现实时生成并下载超大数据量的EXCEL文件详解
Oct 23 PHP
PHP+MySQL高并发加锁事务处理问题解决方法
Apr 30 PHP
Laravel关联模型中过滤结果为空的结果集(has和with区别)
Oct 18 PHP
PHP面向对象程序设计之接口的继承定义与用法详解
Dec 20 PHP
php中array_fill函数的实例用法
Mar 02 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迭代器的内部执行过程详解
2013/11/12 PHP
php自定义hash函数实例
2015/05/05 PHP
php比较相似字符串的方法
2015/06/05 PHP
ubuntu下配置nginx+php+mysql详解
2015/09/10 PHP
两种php去除二维数组的重复项方法
2015/11/04 PHP
javascript动态加载三
2012/08/22 Javascript
Web Inspector:关于在 Sublime Text 中调试Js的介绍
2013/04/18 Javascript
js中的数组Array定义与sort方法使用示例
2013/08/29 Javascript
js的Boolean对象初始值示例
2014/03/04 Javascript
javascript用函数实现对象的方法
2015/05/14 Javascript
Three.js基础部分学习
2017/01/08 Javascript
解决JS内存泄露之js对象和dom对象互相引用问题
2017/06/25 Javascript
vue 过滤器filter实例详解
2018/03/14 Javascript
对Vue2 自定义全局指令Vue.directive和指令的生命周期介绍
2018/08/30 Javascript
vue-cli3 从搭建到优化的详细步骤
2019/01/20 Javascript
Angular2实现的秒表及改良版示例
2019/05/10 Javascript
layui实现二维码弹窗、并下载到本地的方法
2019/09/25 Javascript
Vue v-model组件封装(类似弹窗组件)
2020/01/08 Javascript
Vue组件基础用法详解
2020/02/05 Javascript
python用来获得图片exif信息的库实例分析
2015/03/16 Python
初步探究Python程序的执行原理
2015/04/11 Python
Python pass详细介绍及实例代码
2016/11/24 Python
python+pyqt实现12306图片验证效果
2017/10/25 Python
python jieba分词并统计词频后输出结果到Excel和txt文档方法
2018/02/11 Python
python 列表,数组,矩阵两两转换tolist()的实例
2018/04/04 Python
Windows 8.1 64bit下搭建 Scrapy 0.22 环境
2018/11/18 Python
Python文件操作中进行字符串替换的方法(保存到新文件/当前文件)
2019/06/28 Python
wxPython:python首选的GUI库实例分享
2019/10/05 Python
tensorflow使用指定gpu的方法
2020/02/04 Python
如何理解Python中的变量
2020/06/01 Python
mac安装python3后使用pip和pip3的区别说明
2020/09/01 Python
python实现感知机模型的示例
2020/09/30 Python
幼儿园托班开学寄语
2014/01/18 职场文书
质检员岗位职责范本
2015/04/07 职场文书
党小组鉴定意见
2015/06/02 职场文书
一个成功的互联网创业项目,必须满足这些要求
2019/08/23 职场文书