Php创建验证码类

今天我分享一下商城开发的验证码类的封装,比较简易,也不太齐全,希望大神能够帮忙指点:先来一份封装验证码的流程:

封装验证码过程

1.创建画布
resource imagecreatetruecolor ( int $width , int $height )
--------------------------------------------------------------------------------
2.创建画布颜色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
--------------------------------------------------------------------------------
3.填充颜色
bool imagefill ( resource $image , int $x , int $y , int $color )
--------------------------------------------------------------------------------
4.设置验证码库并产生验证码
str ="abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ123456789";

captcha="";
for(){
captcha . = str[mt_rand(0.strlen(str)-1)];
}
egg: 设置多少位循环体内限定
--------------------------------------------------------------------------------
5.设置字体颜色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
--------------------------------------------------------------------------------
6.将验证码写入画布中
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
--------------------------------------------------------------------------------
7.设置干扰线

7.1设置干扰线颜色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
--------------------------------------------------------------------------------
7.2添加干扰线
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
--------------------------------------------------------------------------------
8.设置干扰点

8.1设置干扰点颜色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
--------------------------------------------------------------------------------
8.2添加干扰点
说明
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。

参见 imagecreatetruecolor() 和 imagecolorallocate()。
--------------------------------------------------------------------------------
9.1设置相应头
header('Content-type:image/png');
--------------------------------------------------------------------------------
9.2保存画布
bool imagepng ( resource $image [, string $filename ] )
imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。gif也可以
--------------------------------------------------------------------------------
10.回收资源
bool imagedestroy ( resource $image )
imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,例如 imagecreatetruecolor()。
--------------------------------------------------------------------------------

以上的php函数均可以在php的开发手册里面查询到

下面是我的源代码:

<span style="font-size:18px;"><?php

 	//创建captchaÀà
class Captcha {
 	#2.属性
	private $width ;//验证码图片的宽
	private $height ;//验证码的高
	private $font; //验证码字体的大小,php提供的最大为5
	private $length ;//验证码的字符串的长度
	private $color; //验证码的颜色,此处为一个数组,包括字体颜色,背景颜色,干扰线颜色,干扰点颜色
	private $lines; //干扰线数量
	private $pixels;//干扰点数量
	private $str;   //验证码的字符库

 #3.构造方法
 	/*
 	 *param array $arr 包括所有属性
 	 *
 	 */
 	public function __construct($arr = array()){
 		$this->width 		= isset($arr['width'])?(isset($arr['width'])<146 ?   146: $arr['width']) :146;
 		$this->height 		= isset($arr['height'])?(isset($arr['height'])<20 ?  20: $arr['height']) :20;
 		$this->font 		=  isset($arr['font'])?(isset($arr['font'])<5 ?  5: $arr['font']) :5;
 		$this->length 		=  isset($arr['length'])?$arr['length']:4;
 		$this->str 		 	=  isset($arr['str'])?$arr['str']:"abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ123456789";
 		$this->lines 		=  isset($arr['lines'])?$arr['lines']:5;
 		$this->pixels 		=  isset($arr['pixels'])?$arr['pixels']:150;
 		$this->color['bg_max']		= isset($arr['bg_max'])?(isset($arr['bg_max'])>255 ?  255: $arr['height']) :255;
 		$this->color['bg_min']		= isset($arr['bg_min'])?(isset($arr['bg_min'])<200 ?  200: $arr['height']) :200;
 		$this->color['font_max']		= isset($arr['bg_max'])?(isset($arr['bg_max'])>100 ?  100: $arr['height']) :100;
 		$this->color['font_min']		= isset($arr['bg_min'])?(isset($arr['bg_min'])<0 ?  0: $arr['height']) :0;
 		$this->color['line_max']		= isset($arr['bg_max'])?(isset($arr['bg_max'])>200 ?  200: $arr['height']) :200;
 		$this->color['line_min']		= isset($arr['bg_min'])?(isset($arr['bg_min'])<100 ? 100: $arr['height']) :100;
 		$this->color['pixel_max']		= isset($arr['bg_max'])?(isset($arr['bg_max'])>200 ?  200: $arr['height']) :200;
 		$this->color['pixel_min']		= isset($arr['bg_min'])?(isset($arr['bg_min'])<100 ?  100: $arr['height']) :100;
 	}
 	#4.创建验证码
 	/*
 	 * createCaptcha
 	 *@author  文华
 	 */
 	public function createCaptcha(){
 		//创建画布
 		$img  =  imagecreatetruecolor($this ->width,$this->height);
 		//创建画布颜色
 		$bg_color =imagecolorallocate($img, mt_rand($this->color['bg_min'],$this->color['bg_max']),mt_rand($this->color['bg_min'],$this->color['bg_max']), mt_rand($this->color['bg_min'],$this->color['bg_max']));
 		//填充颜色
 		imagefill($img, 0, 0, $bg_color);
 		
 		#添加干扰线
 		for ($i=0; $i <$this->lines ; $i++) { 
 			//干扰线颜色
 			$line_color = imagecolorallocate($img, mt_rand($this->color['line_min'],$this->color['line_max']),mt_rand($this->color['line_min'],$this->color['line_max']), mt_rand($this->color['line_min'],$this->color['line_max']));
			//添加到画布上
 			imageline($img, mt_rand(0,$this->width), mt_rand(0,$this ->height),mt_rand(0,$this->width), mt_rand(0,$this ->height), $line_color);

 		}

 		#添加干扰点
 		for ($i=0; $i <$this ->pixels ; $i++) { 
 			//干扰点颜色
 			$pixel_color = imagecolorallocate($img, mt_rand($this->color['pixel_min'],$this->color['pixel_max']),mt_rand($this->color['pixel_min'],$this->color['pixel_max']), mt_rand($this->color['pixel_min'],$this->color['pixel_max']));
 			//添加干扰点到画布上
 			imagesetpixel($img, mt_rand(0,$this->width), mt_rand(0,$this->height), $pixel_color);
 		}

 		//获取验证码字符串
 		$captcha = $this ->getCaptchaString($this ->str,$this->length);
 		//蛇珠验证码字符串颜色
 		$font_color =imagecolorallocate($img, mt_rand($this->color['font_min'],$this->color['font_max']),mt_rand($this->color['font_min'],$this->color['font_max']), mt_rand($this->color['font_min'],$this->color['font_max']));
 		//添加验证码到画布上
 		imagestring($img, $this ->font, ceil(($this->width)/2-20), ceil(($this->height)/2-8), $captcha,$font_color);


 		//输出图片
 		header('Content-type:image/png');
 		#如果保存图,有两个参数,后一个参数为地址
 		imagepng($img);

 		//回收资源
 		imagedestroy($img);

 	}
 	/*获取验证码字符串
 	 *@param1 int $length 字符串长度
  	 *@param2 string $str 字符串库
 	 *@return string $captchastr  验证码字符串
 	 *
 	 */
 	private function getCaptchaString(){
 		$captchastr="";
 		for($i=0;($i<$this->length);$i++){
 			$captchastr .= $this ->str[mt_rand(0,strlen($this->str)-1)];
 		}
 		$_SESSION['captcha'] = $captchastr;
 		return $captchastr;
 	}
 	/*@param1 string $captcha 用户输入验证码
 	 *
 	 */
 	static public function checkCaptcha($captcha){
 		return (strtolower($captcha)===strtolower($_SESSION['captcha']));
 	}


 }</span>

其中,属性可以用$GLOBALS[]来修改config中相应的值,这个大家可以去尝试下,小弟就不多写了,我也是刚刚起步,希望多交流。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值