PHP验证码类

本文介绍了一个PHP类,用于生成包含数字、字母等不同类型的验证码图片。该类支持自定义图片尺寸、背景颜色、验证码样式及干扰元素等设置。
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,我这儿是做测试用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值