PHP 动态随机生成验证码类代码
2015-01-24信息快讯网
这个利用PHP动态随机生成验证码的类是在LAMP的书上看到的。感觉写的很好就收藏了下来。
下面是效果图,这个效果图是没有开启干扰码的效果图下面是类代码
<?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的朋友 经常问的一些问题。不断更新
php 广告调用类代码(支持Flash调用)
php 中英文语言转换类代码
php中计算中文字符串长度、截取中文字符串的函数代码
PHP FOR MYSQL 代码生成助手(根据Mysql里的字段自动生成类文件的)
一个PHP验证码类代码分享(已封装成类)
PHP 验证码的实现代码
php中比较简单的导入phpmyadmin生成的sql文件的方法
php中随机显示图片的函数代码
php 随机排序广告的实现代码
php中将地址生成迅雷快车旋风链接的代码[测试通过]
php 生成文字png图片的代码
php产生随机数的两种方法实例代码 输出随机IP
PHP随机数生成代码与使用实例分析
php生成随机密码的几种方法
PHP验证码类代码( 最新修改,完全定制化! )
《PHP编程最快明白》第七讲:php图片验证码与缩略图
php 验证码实例代码
dedecms系统的广告设置代码 基础版本
DedeCMS 核心类TypeLink.class.php摘要笔记
通俗易懂的php防注入代码
用PHP实现读取和编写XML DOM代码
php session和cookie使用说明
php抓取https的内容的代码
PHP生成Flash动画的实现代码
通用PHP动态生成静态HTML网页的代码
用PHP ob_start()控制浏览器cache、生成html实现代码
php 生成静态页面的办法与实现代码详细版
PHP生成网页快照 不用COM不用扩展.
php 生成随机验证码图片代码
PHP 获取目录下的图片并随机显示的代码
php5 图片验证码实现代码
php 数学运算验证码实现代码
php 随机数的产生、页面跳转、件读写、文件重命名、switch语句
php 验证码制作(网树注释思想)
PHP n个不重复的随机数生成代码
php 随机生成10位字符代码
PHP生成带有雪花背景的验证码