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 时间计算问题小结
Jan 04 PHP
php垃圾代码优化操作代码
Aug 05 PHP
《PHP编程最快明白》第四讲:日期、表单接收、session、cookie
Nov 01 PHP
PHP递归算法的详细示例分析
Feb 19 PHP
深入理解require与require_once与include以及include_once的区别
Jun 05 PHP
php利用新浪接口查询ip获取地理位置示例
Jan 20 PHP
php基于mcrypt的加密解密实例
Oct 27 PHP
php读取txt文件并将数据插入到数据库
Feb 23 PHP
PHP之图片上传类实例代码(加了缩略图)
Jun 30 PHP
php mysql like 实现多关键词搜索的方法
Oct 29 PHP
PHP实现SMTP邮件的发送实例
Sep 27 PHP
laravel中Redis队列监听中断的分析
Sep 14 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
PHILIPS AE3805收音机的分析打磨
2021/03/02 无线电
PHP5中的this,self和parent关键字详解教程
2007/03/19 PHP
PHP 读取和修改大文件的某行内容的代码
2009/10/30 PHP
php gzip压缩输出的实现方法
2013/04/27 PHP
PHP连接sql server 2005环境配置及问题解决
2014/08/08 PHP
thinkphp缓存技术详解
2014/12/09 PHP
深入理解PHP之OpCode原理详解
2016/06/01 PHP
php图像处理函数imagecopyresampled用法详解
2016/12/02 PHP
利用PHP判断是手机移动端还是PC端访问的函数示例
2017/12/14 PHP
让innerHTML的脚本也可以运行起来
2006/07/01 Javascript
JavaScript 事件查询综合
2009/07/13 Javascript
Js基础学习资料
2010/11/23 Javascript
使用CSS3的scale实现网页整体缩放
2014/03/18 Javascript
jQuery产品间断向下滚动效果核心代码
2014/05/08 Javascript
jQuery新的事件绑定机制on()示例应用
2014/07/18 Javascript
javascript框架设计读书笔记之种子模块
2014/12/02 Javascript
JavaScript实现简洁的俄罗斯方块完整实例
2016/03/01 Javascript
深入理解JavaScript中的并行处理
2016/09/22 Javascript
JavaScript输出所选择起始与结束日期的方法
2017/07/12 Javascript
JS大坑之19位数的Number型精度丢失问题详解
2019/04/22 Javascript
mpvue小程序循环动画开启暂停的实现方法
2019/05/15 Javascript
八种Vue组件间通讯方式合集(推荐)
2020/08/18 Javascript
js实现扫雷源代码
2020/11/27 Javascript
Python实现图像几何变换
2015/07/06 Python
浅析Python中signal包的使用
2015/11/13 Python
详解Python使用Plotly绘图工具,绘制甘特图
2019/04/02 Python
使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解
2020/01/25 Python
python 匿名函数与三元运算学习笔记
2020/10/23 Python
关键字throw与throws的用法差异
2016/11/22 面试题
会计试用期自我评价怎么写
2014/09/18 职场文书
2014教育局对照检查材料思想汇报
2014/09/23 职场文书
工作检讨书范文
2015/01/23 职场文书
2015年度个人思想工作总结
2015/04/08 职场文书
使用CSS实现小三角边框原理解析
2021/11/07 HTML / CSS
MySQL数据库如何给表设置约束详解
2022/03/13 MySQL
详解Mysql数据库平滑扩容解决高并发和大数据量问题
2022/05/25 MySQL