面向对象中的验证码类

<?php

/**
* 验证码类
*/
class Verify
{
	//成员属性
	private $width;			//宽
	private $height;		//高
	private $verify_code;	//验证码字符串
	private $verify_nums;	//验证码个数
	private $verify_type;	//验证码字符类型
	private $image_type;	//图片类型(JPG/PNG)
	private $res;			//图片资源

	//构造函数来执行需要初始化的成员属性
	public function __construct($width = 120,$height = 50,$verify_nums = 4,$verify_type = 3,$image_type = ‘png‘)
	{
		$this->width = $width;
		$this->height = $height;
		$this->verify_nums = $verify_nums;
		$this->verify_type = $verify_type;
		$this->image_type = $image_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 imagecreatetruecolor($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 imagecolorallocate($res,$color1,$color2,$color3);*/

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

	//创建亮色给字符串用
	private function createLightColor()
	{
		return imagecolorallocate($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	imagefill($res,$x,$y,$color);*/
	   	imagefill($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);

			imagesetpixel($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);				//终点角度

	   		imagearc($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(‘请输入指定验证码的字符类型<br/>‘);
		}
		$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);
			imagechar($this->res, 5, $x, $y, $this->verify_code[$i], $this->createLightColor());
		}

	}
	//输出图片
	private function outPutImg()
	{
		header(‘Content-type:image/‘.$this->image_type);

		$func = ‘image‘ . $this->image_type;
		$func($this->res);

	}

	public function __get($name)
	{
		if ($name == ‘verify_code‘) {
			return $this->verify_code;
		}

	}
	//销毁资源
	public function __destruct()
	{
		imagedestroy($this->res);
	}

}

$v = new Verify();

$v->show();
时间: 2024-08-07 04:34:17

面向对象中的验证码类的相关文章

PHP中的验证码类(完善验证码)

运行结果: <!--vcode.class.php--> <?php class Vcode { private $width; //宽 private $height; //高 private $num; //数量 private $code; //验证码 private $img; //图像的资源 //构造方法, 三个参数 function __construct($width=80, $height=20, $num=4) { $this->width = $width; $

PHP中的验证码类(验证码功能设计之二)

运行结果: <!--vcode.class.php内容--> <?php class Vcode { private $width; //宽 private $height; //高 private $num; //数量 private $code; //验证码 private $img; //图像的资源 //构造方法, 三个参数 function __construct($width, $height, $num) { $this->width = $width; $this-&

PHP中的验证码类(验证码功能设计之一)

<!--vcode.class.php内容--> <?php class Vcode { private $width; //宽 private $height; //高 private $num; //数量 private $code; //验证码 //构造方法, 三个参数 function __construct($width, $height, $num) { $this->width = $width; $this->height = $height; $this-&

面向对象中数据库操作类

具体实现功能: 1.连接数据库: 2.插入数据: 3.更新数据: 4.删除数据' 5.修改数据: 6.求最大值: 7.求最小值: 8.求平均数: 9.求和: 10.指定查询: 具体代码分为三个部分: 一.config文件:主要用于连接数据库 <?php return array( 'DB_HOST' => '127.0.0.1',   //主机 'DB_USER' => 'root', //用户名 'DB_PWD' => '123456', //密码 'DB_NAME' =>

PHP中的验证码类(准备篇)

<!--code.php内容--> <?php //开启session session_start(); include "vcode.class.php"; //构造方法 $vcode = new Vcode(80, 25, 4); //将验证码放到服务器自己的空间保存一份 $_SESSION = $vcode->getcode(); //将验证码图片输出 $vcode->outimg(); <!--reg.php内容--> <body

面向对象中的分页类

<?php $page = new Page(53,10); $p = $page->rendor(); echo '<pre>'; var_dump($p); echo'</pre>'; /** * 分页类 */ class Page { protected $url; //URL protected $pageCount; //总页数 protected $total; //总条数 protected $num; //每页显示数 protected $page; /

面向对象中对象和类以及如何访问类中属性和方法

对象:客观存在的具体事物.某一个具体的个体 *类:具有相同行为和共同特征的对象的集合,类是人类脑海中一个抽象的概念,通过类创建对象 *类中的成员:1.成员属性(描述外部特征) 2.成员方法(描述功能行为)* 如何定义一个类: * [修饰符] class 类名{ * //1.属性的定义:与定义变量类似 * [修饰符] 数据类型 属性名; * //2.方法的定义 * [修饰符] 返回值类型 方法名(形参列表){ * //方法体; * } * } * 成员变量和局部变量的区别: * 1.作用域不同:成

面向对象中关于元类的介绍与异常处理

一.异常处理 1. 什么是异常处理     异常是错误发生的信号,一旦程序出错就会产生一个异常,如果该异常     没有被应用程序处理,那么该异常就会抛出来,程序的执行也随之终止 异常包含三个部分:         1. traceback异常的追踪信息         2. 异常的类型         3. 异常的信息 错误分为两大类:         1. 语法上的错误:在程序运行前就应该立即修正         2. 逻辑上的错误 2. 为何要异常处理 避免程序因为异常而崩溃,所以在应用程

JavaScript实现面向对象中的类

对象,是人们要进行研究的任何事物,从最简单的整数到复杂的飞机等均可看作对象,它不仅能表示具体的事物,还能表示抽象的规则.计划或事件.--引自百度百科 面向对象编程,是当前最流行的编程模式.但令人沮丧的是,作为前端应用最为广泛的javascript,并不支持面向对象. javascript没有访问控制符,它没有定义类的关键字class,它没有支持继承的extend或冒号,它也没有用 来支持虚函数的virtual,不过,Javascript是一门灵活的语言,下面我们就看看没有关键字class的Jav