<?php /** * 验证码驱动 * @author Devil * @version v_1.0.0 */ class VerifyLibrary { private $rand_string; private $img; /** * [__construct 构造方法] */ public function __construct() { /* 验证码生成 */ $this->rand_string = $this->GetRandString(); } /** * [GetVerify 获取当前验证码] */ public function GetVerify() { return $this->rand_string; } /** * [GetVerifyImg 验证码生成] * @return [string] [验证码] */ public function GetVerifyImg() { $this->img = imagecreatetruecolor(63, 22); //创建一个画布(真色彩) // 画背景 $back_color = imagecolorallocate($this->img, 235, 236, 237); imagefilledrectangle($this->img,0,0,63,22,$back_color); //加入干扰,画出多条线 $this->InterferenceLine(); //加入干扰,画出点 $this->InterferencePoint(); //将生成好的字符串写入图像 $fgcolor = imagecolorallocate($this->img, rand(0,200), rand(0,255), rand(0,255)); imagestring($this->img, 5, 5, 5, strtoupper($this->rand_string), $fgcolor); //输出图像 header('Content-Type: image/gif'); imagegif($this->img); //销毁图像 imagedestroy($this->img); } /** * [InterferencePoint 加入干扰,画点] */ private function InterferencePoint() { for($i=0; $i<200; $i++){ $bgcolor = imagecolorallocate($this->img, rand(0,255), rand(0,255), rand(0,255)); //产生随机的颜色 imagesetpixel($this->img, rand()%90, rand()%30, $bgcolor); } } /** * [InterferenceLine 加入干扰,画出多条线] */ private function InterferenceLine() { for($i=0; $i<5; $i++) { $bgcolor=imagecolorallocate($this->img, rand(0,255), rand(0,255), rand(0,255)); //产生随机的颜色 imageline($this->img, rand(10,90), 0, rand(10,90), 20, $bgcolor); } } /** * [GetRandString 生成随机数值] * @param [int] $number [随机数位数] * @return[string] [返回小写的随机数值] */ private function GetRandString($number = 6) { $origstr = '3456789abxdefghijkmnprstuvwxy'; $verifystring = ''; $len = strlen($origstr); for($i=0; $i<$number; $i++) { $index = mt_rand(0, $len-1); $char = $origstr[$index]; $verifystring .= $char; } return $verifystring; } } ?>