下面是类代码
复制代码 代码如下:
<?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();
*******************************************************************/
?>
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!