【PHP】一个简单的验证码类

初学Php,因为很多地方都涉及到验证码的功能,网上虽然有很多验证码的例子,但是看了之后都不是很满意。于是,自己随便看了一下书,写了一个简单地Php验证码类,可以提供简单的生成一个验证码类。当然,这个类也是刚刚写好,还有很多可以改进的地方。

下面是代码。

<?php
/*
	2014/3/28
	@程序与算法
*/
class checkCode{
	private $m_fontFile = "";  //储存引入的字体文件路径
	private $m_fontSize = 16;  // 字体大小
	private $m_str = "";  //储存生成验证码的字符串
	private $m_width = 80; //验证码图片的宽度
	private $m_heigh =  30; //验证码图片的高度
	private $m_bg = array("0xff", "0xff", "0xff");  //默认的背景颜色
	private $m_tg = array("0x00", "0x00", "0x00");   //默认的字体颜色
	private $m_ig;  //图片变量
	private $m_isbg = "";
	private $m_istg = "";
	private $m_codearray = array();  //动态生成验证码的字符数组,这里是0-9,a-z,A-Z
	private $m_codeNum = 4; //验证码的长度
	
	//构造函数,可以传入宽度和高度,也可以使用默认的值
	public function __construct($m_w = "", $m_h = "") 
	{
		$this->createCodeArray();  //用于生成随机字符串
		if ($m_w == "" || $m_h == "")
		{
			$this->m_ig = imagecreate($this->m_width, $this->m_heigh);
		}
		else
		{
			$this->m_ig = imagecreate($m_w, $m_h);
			$this->m_width = $m_w;
			$this->m_heigh = $m_h;
		}
		$this->setBgColor($this->m_bg[0], $this->m_bg[1], $this->m_bg[2]);
	}
	public function __destruct()
	{
		imagedestroy($this->m_ig);
	}
	
	public function setBgColor($m_r, $m_g, $m_b)  //设置验证码的背景颜色
	{

		$this->m_ig = imagecreate($this->m_width, $this->m_heigh);
		$this->m_isbg = imagecolorallocate($this->m_ig, $m_r, $m_g, $m_b);
		return $this->m_isbg;
	}
	public function setTxColor($m_r, $m_g, $m_b)  //设置字体颜色
	{
		$this->m_istg = imagecolorallocate($this->m_ig, $m_r, $m_g, $m_b);
		return $this->m_istg;
	}
	public function setCodeCount($m_num) //设置验证码的长度
	{
		$this->m_codeNum = $m_num;
	}
	// 设置验证码的字符串,这个字符串可以是用户自己设置的,也可以是由系统自动生成随机的字符串
	public function setCodeStr($str)  
	{
		$this->m_str = $str;
	}
	public function setCodeFontSize($size) //设置验证码字体的大小
	{
		$this->m_fontSize = $size;
	}
	public function setCodeFontFile($file) //设置字体类型,如果不调用这个函数,就使用默认的字体
	{
		if (file_exists($file))
		{
			$this->m_fontFile = $file;
		}
	}
	public function createCodeArray()  //生成随机字符串的数组
	{
		$m_aa = ord(‘A‘);
		$m_AB = ord(‘a‘);
		for ($m_i = 0; $m_i < 36; ++ $m_i)
		{
			if ($m_i >= 0 && $m_i < 10)
			{
				array_push($this->m_codearray, $m_i);
			}
			else
			{
				$m_temp = chr($m_aa + $m_i - 10);
				array_push($this->m_codearray, $m_temp);
				$m_temp = chr($m_AB + $m_i - 10);
				array_push($this->m_codearray, $m_temp);
			}
		}
	}
	public function createMass()  //生成杂点
	{
		$m_line = imagecolorallocate($this->m_ig, 0xdd, 0xdd, 0xdd);
		for ($m_i = 0; $m_i < ($this->m_width + $this->m_heigh) / 50; ++ $m_i)
		{
			$m_fx = rand(0, $this->m_width);
			$m_fy = rand(0, $this->m_heigh);
			$m_sx = rand(0, $this->m_width);
			$m_sy = rand(0, $this->m_heigh);
			imageline($this->m_ig, $m_fx, $m_fy, $m_sx, $m_sy, $m_line);
		}
		$m_point = imagecolorallocate($this->m_ig, 0x00, 0x00, 0x00);
		for ($m_i = 0; $m_i < $this->m_width * $this->m_heigh / 50; ++ $m_i)
		{
			$m_fx = rand(0, $this->m_width);
			$m_fy = rand(0, $this->m_heigh);
			imagesetpixel($this->m_ig, $m_fx, $m_fy, $m_point);
		}
	}
	private function deutCode()  //默认字体生成的验证码图片
	{
		@$m_arrstr = str_split($this->m_str); 
		//$this->createMass();
		if ($this->m_str == "") 
		{
		
			for ($m_i = 0; $m_i < $this->m_codeNum; ++ $m_i)
			{
				$m_index = rand(0, count($this->m_codearray) - 1);
				$m_tempx = ($this->m_width / $this->m_codeNum) * ($this->m_codeNum - 1) / $this->m_codeNum;
				$m_w = $m_tempx * ($m_i + 1);
				$m_dh = $this->m_heigh / 4;
				$m_h = rand($m_dh * 0.6, $m_dh * 1.5);
				imagestring($this->m_ig, $this->m_fontSize, $m_w, $m_h, $this->m_codearray[$m_index], $this->m_istg);
			}
		}
		else
		{
			$m_len = count($m_arrstr);
			for ($m_i = 0; $m_i < $m_len; ++ $m_i)
			{
				$m_tempx = ($this->m_width / $m_len) * ($m_len - 1) / $m_len;
				$m_w = $m_tempx * ($m_i + 1);
				$m_dh = $this->m_heigh / 4;
				$m_h = rand($m_dh * 0.6, $m_dh * 1.5);
				imagestring($this->m_ig, $this->m_fontSize, $m_w, $m_h, $m_arrstr[$m_i], $this->m_istg);
			}
		}
		imagepng($this->m_ig);
	}
	private function chgeCode()  //新字体生成的验证码图片
	{
		@$m_arrstr = str_split($this->m_str);
		if ($this->m_str == "")
		{
			for ($m_i = 0; $m_i < $this->m_codeNum; ++ $m_i)
			{
				$m_index = rand(0, count($this->m_codearray) - 1);
				$m_tempx = ($this->m_width / $this->m_codeNum) * ($this->m_codeNum - 1) / $this->m_codeNum;
				$m_w = $m_tempx * ($m_i + 1);
				$m_h = $this->m_heigh / 1.2;
				$m_angel = rand(0, 60);
				imagefttext($this->m_ig, $this->m_fontSize, $m_angel, $m_w, $m_h, $this->m_istg, $this->m_fontFile, $this->m_codearray[$m_index]);
			}
		}
		else
		{
			$m_len = count($m_arrstr);
			for ($m_i = 0; $m_i < $m_len; ++ $m_i)
			{
				$m_tempx = ($this->m_width / $m_len) * ($m_len - 1) / $m_len;
				$m_w = $m_tempx * ($m_i + 1);
				$m_h = $this->m_heigh / 1.2;
				$m_angel = rand(0, 45);
				imagefttext($this->m_ig, $this->m_fontSize, $m_angel, $m_w, $m_h, $this->m_istg, $this->m_fontFile, $m_arrstr[$m_i]);
			}
		}
	}

	public function getCode()  //获取验证码的函数,这个函数就是相当于生成验证码
	{
		if ($this->m_istg == "")
		{
			$this->setTxColor($this->m_tg[0], $this->m_tg[1], $this->m_tg[2]);
		}
		$this->createMass();   //这个函数下面会有说明是一个生成随机杂点的函数
		if ($this->m_fontFile == "")
		{
			$this->deutCode();
		}
		else
		{
			$this->chgeCode();
		}
		imagepng($this->m_ig);
	}
}
?>

该类的使用方法一,这是最简单的形式。

1. 首先引入该类

2. 初始化,不想类传递任何参数,全部使用默认、

3. 生成验证码

具体的代码如下

<?php include "checkCode.php";?>
<?php
	header("Content-Type:image/png");
	$m_code = new checkCode();
	$m_code->getCode();
?>
方法二,通过传递参数,生成更加漂亮的验证码。(推荐使用这个方法,因为系统默认的字体不是很清晰而且比较小)

<?php include "checkCode.php";?>
<?php
	header("Content-Type:image/png");
	$m_code = new checkCode(100, 50); // 或者 $m_code = new checkCode(200, 50);传递宽度和高度
	$m_code->setCodeFontFile("Arial.ttf"); //引入新的字体
	$m_code->setCodeFontSize(22);  //设置字体大小
	$m_code->setCodeCount(5);  //设置验证码的长度
	//背景颜色参数为RGB的分量值, 0-255 使用十六进制 0x00-0xff
	$m_code->setBgColor(0xdd, 0xcc, 0xcc);  
	$m_code->setTxColor(0x00, 0x11, 0x22);  //,字体颜色同上
	$m_code->getCode();
?>

效果图。

【PHP】一个简单的验证码类

上述的介绍中,还可以使用一个函数

public function setCodeStr($str)  
{
$this->m_str = $str;
}

这个函数用于接收用户传进来的字符串,比如说,$m_code->setCodeStr("12345678"); 

那么生成的图片里面的字符就是 12345678

其实,在那个生成杂点的函数做的不是很好,可以再次接受用户的输入,来生成杂点。可以让用户来选择设置杂点的颜色,杂店的数量等等。。这些还是需要解决的。由于时间紧促,只能先把这个类实现到这个样子了。

【PHP】一个简单的验证码类,布布扣,bubuko.com

【PHP】一个简单的验证码类

上一篇:MySQL的SQL语句 - 数据操作语句(13)- 子查询(6)


下一篇:ThinkPHP5分布式数据库读写分离