PHP中面向对象的验证码类
admin
2023-06-21 17:41:50
0
width = $width;
		$this->height = $height;
		$this->verify_nums = $verify_nums;
		$this->verify_type = $verify_type;
		$this->p_w_picpath_type = $p_w_picpath_type;
		$this->verify_code = $this->setVerifyCode();
		$this->res = $this->createRes();
	}

	//唯一供别人调用的输出方法
	public function show()
	{
		$this->createRes();
		$this->fillBackground();
		$this->fillPix();
		$this->fillArc();
		$this->fillVerifyCode();
		$this->outPutImg();

	}

	//准备画布
	private function createRes()
	{
		return p_w_picpathcreatetruecolor($this->width, $this->height);
	}

	//创建颜色(深色、亮色)
	private function createDarkColor()
	{
		/*$res = $this->res;
		$color1 = mt_rand(0,120);	//red
		$color2 = mt_rand(0,120);	//green
		$color3 = mt_rand(0,120);	//blue
		return p_w_picpathcolorallocate($res,$color1,$color2,$color3);*/

		return p_w_picpathcolorallocate($this->res,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
	}

	//创建亮色给字符串用
	private function createLightColor()
	{
		return p_w_picpathcolorallocate($this->res,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
	}

	//填充背景色
	private function fillBackground()
	{
		/*$res = $this->res;
		$x = 0;
		$y = 0;
		$color = $this->createDarkColor();
	    return	p_w_picpathfill($res,$x,$y,$color);*/
	   	p_w_picpathfill($this->res,0,0,$this->createDarkColor());
	}
	//添加干扰元素
	private function fillPix()
	{
		$nums = ($this->width * $this->height) / 20;

		for ($i=0; $i < $nums; $i++) {

			$x = mt_rand(0,$this->width);

			$y = mt_rand(0,$this->height);

			p_w_picpathsetpixel($this->res, $x, $y, $this->createDarkColor());
		}

	}
	//添加干扰弧线
	private function fillArc()
	{
		for ($i=0; $i < 20 ; $i++) {

			$cx = mt_rand(0,$this->width);		//中心点x坐标
			$cy = mt_rand(0,$this->height);		//中心点y坐标
			$w = mt_rand(10,$this->width/2);	//弧线宽度
			$h = mt_rand(5,$this->height/2);	//弧线高度
			$s = mt_rand(0,180);				//起点角度
			$e = mt_rand(181,360);				//终点角度

	   		p_w_picpatharc($this->res,$cx,$cy,$w,$h,$s,$e,$this->createDarkColor());
		}

	}
	//产生验证码字符
	private function setVerifyCode()
	{
		$verify_code = '';
		switch ($this->verify_type) {
			case 1:
				//纯数字的验证码取4个
				$str = join('',array_flip(range(0,9),$this->verify_nums));
				break;
			case 2:
				$str = join('',array_rand(array_flip(range('a','z')),$this->verify_nums));
				break;
			case 3:
				$str = 'qwertyuiplkjhgfdsaxcvbnmQWERTYUPLKJHGFDSAXCVBNM23456789';
				$str = substr(str_shuffle($str), 0,$this->verify_nums);
				break;
			default:
				die('请输入指定验证码的字符类型
'); } $verify_code = $str; return $verify_code; } //把字符串弄到画布上 $this->fillVerifyCode = 'abda' //弱类型语言就比较随意 private function fillVerifyCode() { for ($i=0; $i < $this->verify_nums; $i++) { $every_width = $this->width /$this->verify_nums; /*mt_rand($every_width * 0,$every_width); mt_rand($every_width * 1,$every_width); mt_rand($every_width * 2,$every_width); mt_rand($every_width * 3,$every_width); $x = mt_rand((($this-width / $this->verify_nums) * $i) + 5,($this-width / $this->verify_nums) * ($i+1) - 5) ;*/ $x = mt_rand($every_width * $i + 10 ,$every_width * ($i + 1) -10); $y = mt_rand(10,$this->height - 20); p_w_picpathchar($this->res, 5, $x, $y, $this->verify_code[$i], $this->createLightColor()); } } //输出图片 private function outPutImg() { header('Content-type:p_w_picpath/'.$this->p_w_picpath_type); $func = 'p_w_picpath' . $this->p_w_picpath_type; $func($this->res); } public function __get($name) { if ($name == 'verify_code') { return $this->verify_code; } } //销毁资源 public function __destruct() { p_w_picpathdestroy($this->res); } } $v = new Verify(); $v->show();


相关内容

热门资讯

荷兰军用直升机在南海遭中国军队... 5月28日下午,国防部举行例行记者会。国防部新闻发言人蒋斌大校就“荷兰直升机侵闯中国西沙群岛领空”等...
一日售罄,首批商业不动产REI... 【大河财立方 记者 孙凯杰】5月25日、27日,首批4只商业不动产公募REITs相继面向公众投资者发...
五位民营企业家亮相国新办,两位... 【大河财立方 记者 程帅星 王宁宁 北京报道】 5月28日,国务院新闻办公室举行“新征程上的奋斗者”...
被司法部点赞的安阳涉企检查,是... 【大河财立方 记者 丁洋涛】涉企行政执法专项行动,旨在持续优化营商环境,纵深推进全国统一大市场建设。...
国防部:“新型军国主义”大有成... 5月28日下午,国防部举行例行记者会,国防部新闻发言人蒋斌大校答记者问。有记者提问,据报道,美日近期...
胡衡华辞去重庆市市长职务 5月28日,据“重庆人大”官网消息,重庆市人民代表大会常务委员会发布关于接受胡衡华辞职的决定:根据《...
汇聚政企民合力 共建有温度的许... 大河网讯 “遇到违法和不良信息,请拨打12377。”近期,不少途经许昌东站、胖东来各大门店的市民,都...
日本意图构建国家级情报统合体系... 澎湃新闻记者 杨文钦 朱郑勇5月28日,外交部发言人毛宁主持例行记者会。有记者提问,据报道,近日日本...
“中国登月计划正稳步推进”,美... 【文/观察者网 陈思佳】近年来,美国政客频频炒作美中“登月竞赛”,不仅寻求抢在中国之前完成本世纪首次...
新亚光取得导线防风偏装置专利,... 国家知识产权局信息显示,广东新亚光电缆股份有限公司取得一项名为“一种导线防风偏装置”的专利,授权公告...