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正则
Jul 07 PHP
PHP编程与应用
Oct 09 PHP
我的群发邮件程序
Oct 09 PHP
PHP中uploaded_files函数使用方法详解
Mar 09 PHP
FirePHP 推荐一款PHP调试工具
Apr 23 PHP
php whois查询API制作方法
Jun 23 PHP
深入for,while,foreach遍历时间比较的详解
Jun 08 PHP
解析:使用php mongodb扩展时 需要注意的事项
Jun 18 PHP
php修改指定文件后缀的方法
Sep 11 PHP
PHP Mysqli 常用代码集合
Nov 12 PHP
PHP流Streams、包装器wrapper概念与用法实例详解
Nov 17 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
PHP4引用文件语句的对比
2006/10/09 PHP
PHP strtr() 函数使用说明
2008/11/21 PHP
php xml留言板 xml存储数据的简单例子
2009/08/24 PHP
PHP时间戳与日期之间转换的实例介绍
2013/04/19 PHP
php环境下利用session防止页面重复刷新的具体实现
2014/01/09 PHP
php开发工具有哪五款
2015/11/09 PHP
php将文件夹打包成zip文件的简单实现方法
2016/10/04 PHP
Yii 2.0如何使用页面缓存方法示例
2017/05/23 PHP
Yii2框架自定义类统一处理url操作示例
2019/05/25 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
2020/02/16 PHP
Javascript模块化编程详解
2014/12/01 Javascript
兼容主流浏览器的JS复制内容到剪贴板
2014/12/12 Javascript
浅析四种常见的Javascript声明循环变量的书写方式
2015/10/14 Javascript
url传递的参数值中包含&amp;时,url自动截断问题的解决方法
2016/08/02 Javascript
vue-cli的webpack模板项目配置文件分析
2017/04/01 Javascript
详解CommonJS和ES6模块循环加载处理的区别
2018/12/26 Javascript
vue基础之事件v-onclick=&quot;函数&quot;用法示例
2019/03/11 Javascript
通过实例解析js简易模块加载器
2019/06/17 Javascript
微信小程序实现手势滑动卡片效果
2019/08/26 Javascript
js基于canvas实现时钟组件
2021/02/07 Javascript
关于Python中Inf与Nan的判断问题详解
2017/02/08 Python
用python做一个搜索引擎(Pylucene)的实例代码
2017/07/05 Python
python如何派生内置不可变类型并修改实例化行为
2018/03/21 Python
Python文本处理之按行处理大文件的方法
2018/04/09 Python
使用python将请求的requests headers参数格式化方法
2019/01/02 Python
Python3多线程基础知识点
2019/02/19 Python
python获取指定日期范围内的每一天,每个月,每季度的方法
2019/08/08 Python
python实现图片,视频人脸识别(opencv版)
2020/11/18 Python
如何利用find命令查找文件
2016/11/18 面试题
奥巴马演讲稿
2014/01/08 职场文书
宿舍违规检讨书
2014/01/12 职场文书
医德考评自我评价
2014/09/14 职场文书
2015年机关党建工作总结
2015/05/22 职场文书
生死牛玉儒观后感
2015/06/11 职场文书
解决Mysql的left join无效及使用的注意事项说明
2021/07/01 MySQL
MySQL连接控制插件介绍
2021/09/25 MySQL