您好,登錄后才能下訂單哦!
<?php /** * 驗證碼類 */ class Verify { //成員屬性 private $width; //寬 private $height; //高 private $verify_code; //驗證碼字符串 private $verify_nums; //驗證碼個數 private $verify_type; //驗證碼字符類型 private $p_w_picpath_type; //圖片類型(JPG/PNG) private $res; //圖片資源 //構造函數來執行需要初始化的成員屬性 public function __construct($width = 120,$height = 50,$verify_nums = 4,$verify_type = 3,$p_w_picpath_type = 'png') { $this->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('請輸入指定驗證碼的字符類型<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); 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();
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。