php生成验证码

最终样式:

目录

一、成员属性

1.验证码相关

2.图片相关

二、方法

1.构造函数

2.成员方法

2-1.验证码相关

2-2.图片相关

2-3.将验证码写入图片,并设置输出

2-4.对外接口

3.析构函数

三、代码


一、成员属性

1.验证码相关

个数、类型(纯数字、纯字母、数字和字母混合)以及最终输出的字符串。

protected $number验证码个数
protected $codeType验证码类型
protected $code验证码字符串

 

 

 

2.图片相关

图片资源、宽度、高度

protected $image图像资源
protected $width图像宽度
protected $height图像高度

 

 

 

二、方法

1.构造函数

需要接收的参数:验证码个数、类型、图片宽度、图片高度

需要输出的参数:验证码个数、类型、图片宽度、图片高度、验证码字符串。

2.成员方法

2-1.验证码相关

protected function createCode()判断验证码类型,最终输出验证码字符串$code
protected function getNumberCode()纯数字组合。例如:8754 (个数由$number决定)
protected function getCharCode()纯字母组合,包含大小写。例如:sdKn (个数由$number决定)
protected function getCharNumCode()数字+大小写字母组合。例如:6Uj9 (个数由$number决定)

 

 

 

 

2-2.图片相关

protected function createImage()imagecreatetruecolor()生成图片资源
protected function fillBack()imagefill()图片填充色
protected function deepColor()imagecolorallocate()深色
protected function lightColor()imagecolorallocate()浅色
protected function drawDisturb()imagesetpixel()干扰点

 

 

 

 

 

2-3.将验证码写入图片,并设置输出

protected function drawChart()imagechar()将验证码写入图片
protected function show()设置header()和imagepng()

 

 

2-4.对外接口

public function outImage()

依次调用createImage()创建画布;fillBack()填充画布颜色;drawChart()验证码写入图片;drawDisturb()添加干扰元素;show()输出

 

3.析构函数

public function __destruct()imagedestroy()销毁图像资源

三、代码

<?php
$code = new Code(4,2,100,40);
$code -> outImage();

class Code{
	protected $number;//验证码个数
	protected $codeType;//验证码类型
	protected $width;//图像宽度
	protected $height;//图像高度
	protected $image;//图像资源
	protected $code;//验证码字符串

	public function __construct($number=4,$codeType=2,$width=100,$height=50){
		//初始化自己的成员属性
		$this->number = $number;
		$this->codeType = $codeType;
		$this->width = $width;
		$this->height = $height;

		$this->code = $this->createCode();
	}
	public function __destruct(){
		imagedestroy($this->image);
	}

	//这个方法不用公开,因为它只在内部服务,外部不需要调用这个方法
	protected function createCode(){
		switch ($this->codeType) {//规定0纯数字;1纯字母;2字母数字混合
			case 0:
				$code = $this ->getNumberCode();
				break;
			case 1:
				$code = $this->getCharCode();
				break;
			case 2:
				$code = $this->getCharNumCode();
				break;
			default:
				die('不支持的验证码类别<br/>');
				break;
		}
		return $code;
	}

	protected function getNumberCode(){
		$str = join('',range(0,9));//生成0-9字符串
		$str = str_shuffle($str);//打乱字符串
		$str = substr($str, 0,$this->number);//截取字符串
		return $str;
	}

	protected function getCharCode(){
		$str = join('',range('a','z'));
		$str = $str.strtoupper($str);//小写拼接上[大写]
		$str = str_shuffle($str);//打乱
		$str = substr($str, 0,$this->number);//截取
		return $str;
	}
	protected function getCharNumCode(){
		$str1 = $this->getNumberCode();
		$str2 = $this->getCharCode();
		$str = $str1.$str2;
		return substr(str_shuffle($str), 0,$this->number);
	}
	protected function createImage(){
		$this->image = imagecreatetruecolor($this->width, $this->height);
	}
	protected function fillBack(){
		imagefill($this->image, 0, 0, $this->lightColor());
	}
	protected function lightColor(){
		return imagecolorallocate($this->image, mt_rand(130,255), mt_rand(130,255), mt_rand(130,255));
	}
	protected function deepColor(){
		return imagecolorallocate($this->image,  mt_rand
			(0,120), mt_rand(0,120), mt_rand(0,120));
	}
	protected function drawChart(){
		$width = floor($this->width / $this->number);
		for($i=0;$i<$this->number;$i++){
			imagechar($this->image, 5, mt_rand(($width*$i)+10,($width * ($i+1))-10), mt_rand(10,($this->height)-20), $this->code[$i], $this->deepColor());
		}
	}
	protected function drawDisturb(){
		for($i=0;$i<150;$i++){
			$x = mt_rand(0,$this->width);
			$y = mt_rand(0,$this->height);
			imagesetpixel($this->image, $x, $y, $this->lightColor());
		}
	}
	protected function show(){
		header('Content-type: image/png');
		imagepng($this->image);
	}
	//输出
	public function outImage(){
		//创建画布
		$this->createImage();
		//填充画布背景色
		$this->fillBack();
		//验证码字符串画到画布中
		$this->drawChart();
		//添加干扰元素
		$this->drawDisturb();
		//输出
		$this->show();
	}
}
?>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值