很实用的一个php验证码类_PHP教程

原创
2016-07-13 17:10:17 508浏览

一个比较实用的php图形验证码生成类,调用方法也很简单的,有需要的朋友可以参考一下。

代码如下 复制代码

class ImageCode{
private $width;//验证码图片宽度
private $height;//验证码图片高度
private $codeNum;//验证码字符个数
private $checkCode;//验证码字符
private $image;//验证码画布
function __construct($width=60,$height=20,$codeNum=4){
$this->width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
$this->checkCode=$this->createCheckCode();
}

function getcreateImage(){
$this->getcreateImage();
$this->outputText();
$this->setDisturbColor();
$this->outputImage();
}
function getCheckCode(){
return $this->checkCode;
}

private function getCreateImage(){
$this->image=imagecreatetruecolor($this->width,$this->height);
$black=imagecolorallocate($this->image,255,255,255,0);
$border=imagecolorallocate($this->image,255,255,255,255);
imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
}

private function createCheckCode(){
for($i=0;$icodeNum;$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 $color=imagecolorallocate($this->image,255,255,255);
imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
}
}

private function outputImage(){
if(imagetypes()&IMG_GIF){
header("Content_type:image/gif");
imagegif($this->image);
}elseif(imagetypes()&IMG_JGP){
header("Content_type:image/jpeg");
imagejpeg($this->image,"",0.5);
}else{
die("PHP不支持图像创建");
}
}

function __destruct(){
imagedestroy($this->image);
}
}

?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/629693.htmlTechArticle一个比较实用的php图形验证码生成类,调用方法也很简单的,有需要的朋友可以参考一下。 代码如下 复制代码 ?php classImageCode{ private $widt...
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。