class capcha{
private $code;//验证码
private $bgColor;
private $codeType = 1;//验证码类型,0-大小写字母加数字,1-数字,2-小写英文,3-大写英文,4小写英文加数字、5-大写英文加数字、6-大小写英文
private $isPixel;//是否加干扰点
private $pixelLevel;//干扰点的数量
private $isLine;//是否加干扰线
private $lineLevel;//干扰先数量
private $width;//图片宽度
private $height;//图片高度
private $codeLen;//验证码长度
private $imageType;//图片类型
private $img;//图片对象
private $r;//背景色的十进制
private $g;//背景色的十进制
private $b;//背景色的十进制
public function __construct($bgColor='#ffffff',$codeType=0,$codeLen=4,$width=100,$height=40,$isPixel=TRUE,$pixelLevel=30
,$isLine=TRUE,$lineLevel=5,$imageType='png',$fontSize=20){
$this->bgColor = $bgColor;
$this->codeType = $codeType;
$this->codeLen = $codeLen;
$this->width = $width;
$this->height = $height;
$this->imageType = $imageType;
$this->font = 'consola.ttf';
$this->fontSize = $fontSize;
$this->isPixel = $isPixel;
$this->pixelLevel = $pixelLevel;
$this->isLine = $isLine;
$this->lineLevel = $lineLevel;
$this->createCode();//在初始化函数里生成验证码,方便存入session,如果只在生成图片时生成调用此函数,则无法获取验证码
}
//生成验证码
private function createCode(){
$c1 = '1234567890';
$c2 = 'abcdefghijklmnopqrstuvwxyz';
$c3 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if($this->codeType==0)$c=$c1.$c2.$c3;
if($this->codeType==1)$c=$c1;
if($this->codeType==2)$c=$c2;
if($this->codeType==3)$c=$c3;
if($this->codeType==4)$c=$c1.$c2;
if($this->codeType==5)$c=$c1.$c3;
if($this->codeType==6)$c=$c2.$c3;
for($i=0;$i<$this->codeLen;$i++){
$this->code .= $c[mt_rand(0,strlen($c)-1)];
}
}
//创建验证码背景
private function createBg(){
//将十六进制转为rgb
$this->r = hexdec(substr($this->bgColor,1,2));
$this->g = hexdec(substr($this->bgColor,3,2));
$this->b = hexdec(substr($this->bgColor,5,2));
$this->img = imagecreatetruecolor($this->width,$this->height);
$bg = imagecolorallocate($this->img,$this->r,$this->g,$this->b);
imagefill($this->img,0,0,$bg);
}
//图片中加入生成的验证码
private function createFont(){
$_x = $this->width / $this->codeLen;
for($i=0;$i<$this->codeLen;$i++){
$fg = imagecolorallocate($this->img,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
imagettftext($this->img,$this->fontSize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$fg,$this->font,$this->code[$i]);
}
}
//创建干扰点
private function createPixel(){
for($i=0;$i<$this->pixelLevel;$i++){
$color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
}
//创建干扰线
private function createLine(){
for($i=0;$i<$this->lineLevel;$i++){
$x1 = mt_rand(0,$this->width);
$x2 = mt_rand(0,$this->width);
$y1 = mt_rand(0,$this->height);
$y2 = mt_rand(0,$this->height);
$color = imagecolorallocate($this->img,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));
imageline($this->img,$x1,$y1,$x2,$y2,$color);
}
}
//图片输出控制,比如格式
private function outPut(){
header('Content-Type:image/'.$this->imageType);
switch($this->imageType){
case 'jpg':imagejpg($this->img);break;
case 'png':imagepng($this->img);break;
case 'gif':imagegif($this->img);break;
default:imagepng($this->img);break;
}
imagedestory($this->img);
}
//图片输出函数
public function img(){
$this->createBg();
$this->createFont();
if($this->isPixel)
$this->createPixel();
if($this->isLine)
$this->createLine();
$this->outPut();
}
//验证码输出函数
public function c(){
return strtolower($this->code);
}
}
/************************************************
*使用方法
**************************************************/
$c = new capcha;
$c->img();// 生成验证码图片
#echo '<script>alert("'.$c->c().'");</script>';//获取验证码,可以存如session,我这儿是做测试用