您好,登錄后才能下訂單哦!
PHP面向對象簡易驗證碼類如何實現?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!
<?php class authCode { private static $instance = null; #實例對象 private $width = 120; #圖片寬度 private $height = 40; #圖片高度 private $font = 'font/elephant.ttf'; #字體文件路徑 private $fontSize = 14; #字體大小 private $strLen = 6; #字符個數 private $auth_code_str = null; #驗證碼結果 private $imgResult = null; #圖片資源 #入口文件 靜態方法調用 實例化對象 可用 對象方法調用 public static function img() { if (!(self::$instance instanceof self)) { self::$instance = new self(); } return self::$instance; } #隨機顏色 private function randomColor($img = null, $min = 0, $max = 255) { $rgb = []; for ($i = 1; $i <= 3; $i++) { $rgb[] = str_pad(rand($min, $max), 3, 0, STR_PAD_LEFT); } return imagecolorallocate($img, $rgb[0], $rgb[1], $rgb[2]); } #隨機字符串 private function randomStr($num = 4) { if ($num > 0) { $string = array_merge(range('a', 'z'), range(0, 9), range('A', 'Z'), range(0, 9)); for ($i = 1; $i <= $num; $i++) { shuffle($string); $this->auth_code_str .= array_pop($string); } } return $this; } #創建驗證碼 public function createAuthCode(&$codeStr = false) { if (!$this->auth_code_str) { $this->randomStr($this->strLen); } if ($codeStr !== false && empty($codeStr)) { $codeStr = $this->auth_code_str; } else if (!empty($codeStr) && $codeStr !== false) { $this->auth_code_str = $codeStr; } $this->imgResult = imagecreatetruecolor($this->width, $this->height); $background = $this->randomColor($this->imgResult, 200); imagefilledrectangle($this->imgResult, 0, 0, $this->width, $this->height, $background); $y = ($this->height - $this->fontSize); $string = str_split($this->auth_code_str, 1); for ($i = 0; $i < count($string); $i++) { $frontColor = $this->randomColor($this->imgResult, 0, 200); imagefttext($this->imgResult, $this->fontSize, rand(0, 10), ($this->fontSize + 2) * $i + 10, $y, $frontColor, $this->font, $string[$i]); } return $this; } #生成線 public function line($line = 3) { $line = $line ?: 3; for ($i = 1; $i <= $line; $i++) { $lineColor = $this->randomColor($this->imgResult, 0, 200); imageline($this->imgResult, rand(0, $this->width / 5), rand(5, $this->height - 5), rand($this->width / 1.3, $this->width), rand(5, $this->height - 5), $lineColor); } return $this; } #噪點 public function pixel($num = 50){ $num = $num ?: 3; for ($i = 1; $i <= $num; $i++) { $lineColor = $this->randomColor($this->imgResult, 0, 100); imagesetpixel($this->imgResult, rand(0, $this->width), rand(0, $this->height), $lineColor); } return $this; } #設置大小 public function size($width = null, $height = null) { $this->width = $width ?: 120; $this->height = $height ?: 40; return $this; } #設置字體大小 public function fontSize($fontsize = 14) { $this->fontSize = $fontsize ?: 14; return $this; } #設置字體 public function font($file = null) { if (is_null($file) === true) { $this->font = 'font/elephant.ttf'; } else { $this->font = $file; } return $this; } #設置長度 public function strlen($num = null) { $this->strLen = $num ?: 6; return $this; } public function display() { ob_end_flush(); header("content-type:image/jpeg"); imagejpeg($this->imgResult, null, 100); imagedestroy($this->imgResult); exit; } } #簡單調用方法 authCode::img()->createAuthCode()->display(); /* #指定字符串調用 $string = 'abc123'; authCode::img()->createAuthCode($string)->display(); #設置圖片大小、字數、字體大小 authCode::img()->strlen(8)->size(300,100)->fontSize(30)->createAuthCode()->display(); #添加噪點 authCode::img()->createAuthCode()->line()->pixel()->display(); */ ?>
感謝各位的閱讀!看完上述內容,你們對PHP面向對象簡易驗證碼類如何實現大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。