亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > PHP > 正文

PHP編寫的圖片驗證碼類文件分享

2020-03-22 19:20:40
字體:
來源:轉載
供稿:網友
* To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor.Class Image{ private $img; html' target='_blank'>public $width = 85; public $height = 25; public $code; public $code_len = 4; public $code_str = "329832983DSDSKDSLKQWEWQ2lkfDSFSDjfdsfdsjwlkfj93290KFDSKJFDSOIDSLK"; public $bg_color = '#DCDCDC'; public $font_size = 16; public $font = 'font.ttf'; public $font_color = '#000000'; //創建驗證碼餓字符創 public function create_code(){ $code = ''; for( $i=0;$i $this- code_len;$i++ ){ $code .= $this- code_str[mt_rand(0, strlen($this- code_str)-1)]; return $this- code = $code; //輸出圖像 public function getImage(){ $w = $this- width; $h = $this- height; $bg_color = $this- bg_color; $img = imagecreatetruecolor($w, $h); $bg_color = imagecolorallocate($img, hexdec(substr($bg_color, 1,2)), hexdec(substr($bg_color, 3,2)), hexdec(substr($bg_color, 5,2))); imagefill($img, 0, 0, $bg_color); $this- img = $img; $this- create_font(); $this- create_pix(); $this- show_code();
$color = $this- font_color; $font_color = imagecolorallocate($this- img, hexdec(substr($color,1,2)), hexdec(substr($color, 3,2)), hexdec(substr($color,5,2))); $x = $this- width/$this- code_len; for( $i=0;$i $this- code_len;$i++ ){ $txt_color = imagecolorallocate($this- img, mt_rand(0,100), mt_rand(0, 150), mt_rand(0, 200)); imagettftext($this- img, $this- font_size, mt_rand(-30, 30), $x*$i+mt_rand(3, 6), mt_rand($this- height/1.2, $this- height), $txt_color, $this- font , $this- code[$i]); //imagestring($this- img, $this- font_size, $x*$i+mt_rand(3, 6),mt_rand(0, $this- height/4) , $this- code[$i], $font_color); $this- font_color = $font_color; //畫干擾線 public function create_pix(){ $pix_color= $this- font_color; for($i=0;$i $i++){ imagesetpixel($this- img, mt_rand(0, $this- width),mt_rand(0, $this- height), $pix_color); for($j=0;$j $j++){ imagesetthickness($this- img, mt_rand(1, 2)); imageline($this- img, mt_rand(0, $this- width), mt_rand(0, $this- height), mt_rand(0, $this- width), mt_rand(0, $this- height), $pix_color); //得到驗證碼 public function getCode(){ return strtoupper($this- code);
我們再來看一例代碼:生成圖片類型驗證碼,驗證碼包含數字和大寫字母,session中存放md5加密后的驗證碼 * 圖片驗證碼類 * 生成圖片類型驗證碼,驗證碼包含數字和大寫字母,session中存放md5加密后的驗證碼 * 使用方法: * $captcha = new Catpcha(); * $captcha- buildAndExportImage(); * 作 者: luojing * 創建時間: 2013-3-27 上午11:42:12class Captcha { private $width;//寬度 private $height; //高度 private $codeNum;//驗證碼字符數量 private $image;//驗證碼圖像資源 private $sessionKey;//session中保存的名字 private $captcha;//驗證碼字符串 const charWidth = 10;//單個字符寬度,根據輸出字符大小而變 * 創建驗證碼類,初始化相關參數 * @param $width 圖片寬度 * @param $height 圖片高度 * @param $codeNum 驗證碼字符數量 * @param $sessionKey session中保存的名字 function __construct($width = 50, $height = 20, $codeNum = 4, $sessionKey = 'captcha') { $this- width = $width; $this- height = $height; $this- codeNum = $codeNum; $this- sessionKey = $sessionKey; //保證最小高度和寬度 if($height 20) { $this- height = 20; if($width ($codeNum * self::charWidth + 10)) {//左右各保留5像素空隙 $this- width = $codeNum * self::charWidth + 10; * 構造并輸出驗證碼圖片 public function buildAndExportImage() { $this- createImage(); $this- setDisturb(); $this- setCaptcha(); $this- exportImage(); * 構造圖像,設置底色 private function createImage() { //創建圖像 $this- image = imagecreatetruecolor($this- width, $this- height); //創建背景色 $bg = imagecolorallocate($this- image, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255)); //填充背景色 imagefilledrectangle($this- image, 0, 0, $this- width - 1, $this- height - 1, $bg); * 設置干擾元素 private function setDisturb() { //設置干擾點 for($i = 0; $i 150; $i++) { $color = imagecolorallocate($this- image, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200)); imagesetpixel($this- image, mt_rand(5, $this- width - 10), mt_rand(5, $this- height - 3), $color); //設置干擾線 for($i = 0; $i $i++) { $color = imagecolorallocate($this- image, mt_rand(150, 220), mt_rand(150, 220), mt_rand(150, 220)); imagearc($this- image, mt_rand(-10, $this- width), mt_rand(-10, $this- height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $color); //創建邊框色 $border = imagecolorallocate($this- image, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50)); //畫邊框 imagerectangle($this- image, 0, 0, $this- width - 1, $this- height - 1, $border); * 產生并繪制驗證碼 private function setCaptcha() { $str = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; //生成驗證碼字符 for($i = 0; $i $this- codeNum; $i++) { $this- captcha .= $str{mt_rand(0, strlen($str) - 1)}; //繪制驗證碼 for($i = 0; $i strlen($this- captcha); $i++) { $color = imagecolorallocate($this- image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200)); $x = floor(($this- width - 10)/$this- codeNum); $x = $x*$i + floor(($x-self::charWidth)/2) + 5; $y = mt_rand(2, $this- height - 20); imagechar($this- image, 5, $x, $y, $this- captcha{$i}, $color); * 輸出圖像,驗證碼保存到session中 private function exportImage() { if(imagetypes() & IMG_GIF){ header('Content-type:image/gif'); imagegif($this- image); } else if(imagetypes() & IMG_PNG){ header('Content-type:image/png'); imagepng($this- iamge); } else if(imagetypes() & IMG_JPEG) { header('Content-type:image/jpeg'); imagepng($this- iamge); } else { imagedestroy($this- image); die("Don't support image type!"); //將驗證碼信息保存到session中,md5加密 if(!isset($_SESSION)){ session_start(); $_SESSION[$this- sessionKey] = md5($this- captcha); imagedestroy($this- image); function __destruct() { unset($this- width, $this- height, $this- codeNum,$this- captcha);}例三: class ValidationCode { private $width; private $height; private $codeNum; private $image; //圖像資源 private $disturbColorNum; private $checkCode; function __construct($width=80, $height=20, $codeNum=4){ $this- width=$width; $this- height=$height; $this- codeNum=$codeNum; $this- checkCode=$this- createCheckCode(); $number=floor($width*$height/15); if($number 240-$codeNum){ $this- disturbColorNum= 240-$codeNum; }else{ $this- disturbColorNum=$number; //通過訪問該方法向瀏覽器中輸出圖像 function showImage($fontFace=""){ //第一步:創建圖像背景 $this- createImage(); //第二步:設置干擾元素 $this- setDisturbColor(); //第三步:向圖像中隨機畫出文本 $this- outputText($fontFace); //第四步:輸出圖像 $this- outputImage(); //通過調用該方法獲取隨機創建的驗證碼字符串 function getCheckCode(){ return $this- checkCode; private function createImage(){ //創建圖像資源 $this- image=imagecreatetruecolor($this- width, $this- height); //隨機背景色 $backColor=imagecolorallocate($this- image, rand(225, 255), rand(225,255), rand(225, 255)); //為背景添充顏色 imagefill($this- image, 0, 0, $backColor); //設置邊框顏色 $border=imagecolorallocate($this- image, 0, 0, 0); //畫出矩形邊框 imagerectangle($this- image, 0, 0, $this- width-1, $this- height-1, $border); private function setDisturbColor(){ for($i=0; $i $this- disturbColorNum; $i++){ $color=imagecolorallocate($this- image, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($this- image, rand(1, $this- width-2), rand(1, $this- height-2), $color); for($i=0; $i $i++){ $color=imagecolorallocate($this- image, rand(200, 255), rand(200, 255), rand(200, 255)); imagearc($this- image, rand(-10, $this- width), rand(-10, $this- height), rand(30, 300), rand(20, 200), 55, 44, $color); private function createCheckCode(){//這里主要產生隨機碼,從2開始是為了區分1和l $code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ"; $string=''; for($i=0; $i $this- codeNum; $i++){ $char=$code{rand(0, strlen($code)-1)}; $string.=$char; return $string; private function outputText($fontFace=""){ for($i=0; $i $this- codeNum; $i++){ $fontcolor=imagecolorallocate($this- image, rand(0, 128), rand(0, 128), rand(0, 128)); if($fontFace==""){ $fontsize=rand(3, 5); $x=floor($this- width/$this- codeNum)*$i+3; $y=rand(0, $this- height-15); imagechar($this- image,$fontsize, $x, $y, $this- checkCode{$i},$fontcolor); }else{ $fontsize=rand(12, 16); $x=floor(($this- width-8)/$this- codeNum)*$i+8; $y=rand($fontSize+5, $this- height); imagettftext($this- image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this- checkCode{$i}); private function outputImage() { if(imagetypes() & IMG_GIF){ header("Content-Type:image/gif"); imagepng($this- image); }else if(imagetypes() & IMG_JPG){ header("Content-Type:image/jpeg"); imagepng($this- image); }else if(imagetypes() & IMG_PNG){ header("Content-Type:image/png"); imagepng($this- image); }else if(imagetypes() & IMG_WBMP){ header("Content-Type:image/vnd.wap.wbmp"); imagepng($this- image); }else{ die("PHP不支持圖像創建"); function __destruct(){ imagedestroy($this- image);
$code=new ValidationCode(80, 20, 4);$code- showImage(); //輸出到頁面中供 注冊或登錄使用$_SESSION["code"]=$code- getCheckCode(); //將驗證碼保存到服務器PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
97视频免费观看| 欧美激情国内偷拍| 久久久久久久久久久久久久久久久久av| 麻豆一区二区在线观看| 午夜免费久久久久| 欧美国产第二页| 91精品国产乱码久久久久久久久| 国产精国产精品| 欧美黑人极品猛少妇色xxxxx| 精品久久久999| 亚洲石原莉奈一区二区在线观看| 亚洲a成v人在线观看| 国产精品久久久久久久久久免费| 国产suv精品一区二区| 亚洲国产精品大全| 欧美激情成人在线视频| 亚洲欧美中文日韩v在线观看| 久久久国产91| 日韩在线视频线视频免费网站| 欧美午夜影院在线视频| 亚洲精品久久久久国产| 国产丝袜精品第一页| 国内精品模特av私拍在线观看| 欧美中文字幕视频| 色综合五月天导航| 中文字幕日本欧美| 久久亚洲综合国产精品99麻豆精品福利| 久久久www成人免费精品| 日本一欧美一欧美一亚洲视频| 91丝袜美腿美女视频网站| 精品国产乱码久久久久久天美| 91精品中国老女人| 国产精品扒开腿爽爽爽视频| 欧美成人性色生活仑片| 日韩在线视频播放| 亚洲欧美日韩国产中文专区| 久久精品亚洲一区| 国产精品一区二区在线| 欧美亚洲在线视频| 亚洲精品国产精品久久清纯直播| 久久99国产精品久久久久久久久| 国产一区二区三区在线播放免费观看| 4p变态网欧美系列| 亚洲电影免费观看高清完整版在线观看| 日韩经典一区二区三区| 中文字幕亚洲国产| 色妞久久福利网| 国自在线精品视频| 日韩av中文在线| 亚洲无线码在线一区观看| 久久久久久中文| 高清日韩电视剧大全免费播放在线观看| 国产精品美女在线观看| 国产成人久久久精品一区| 色先锋资源久久综合5566| 欧美精品videossex88| 亚洲美女中文字幕| 一区国产精品视频| 精品视频久久久久久| 欧美亚洲视频在线看网址| 国产欧美日韩精品在线观看| 欧美日韩色婷婷| 亚洲天堂精品在线| 亚洲成人激情视频| 欧美日韩中国免费专区在线看| 久久精品99久久香蕉国产色戒| 日韩精品有码在线观看| 亚洲精品日韩在线| 精品久久久中文| 欧美黄色片免费观看| 国产精品最新在线观看| 日韩精品在线视频观看| 中文字幕国产亚洲| 欧美日韩精品二区| 中文精品99久久国产香蕉| 日韩在线观看电影| 伦伦影院午夜日韩欧美限制| 亚洲人精选亚洲人成在线| 国产精品黄页免费高清在线观看| 国模精品视频一区二区三区| 欧美一区二区三区四区在线| 久久网福利资源网站| 91美女片黄在线观看游戏| 亚洲最大福利网| 最新日韩中文字幕| 久久国产精品久久久久久久久久| 国产成人精品久久二区二区| 亚洲第一免费网站| 欧美日韩国产在线看| 国产精品1区2区在线观看| 日韩激情av在线免费观看| 亚洲欧洲国产一区| 国产精品久久久久久久久久三级| 日韩在线资源网| 欧美黄色片免费观看| 成人高清视频观看www| 国产在线拍偷自揄拍精品| 欧美激情精品久久久久| 久久久久久这里只有精品| 日韩一区二区在线视频| 国产精品激情自拍| xxxx欧美18另类的高清| 亚洲一区二区三区四区视频| 久久夜色撩人精品| 日韩美女视频中文字幕| 亚洲第一页自拍| 国产欧美一区二区三区久久| 亚洲国产97在线精品一区| 欧洲午夜精品久久久| 欧美激情在线播放| 富二代精品短视频| 亚洲欧美国内爽妇网| 欧美激情乱人伦| 亚洲国产欧美久久| 国产精品久久久久久久久男| 成人欧美一区二区三区在线| 久久在精品线影院精品国产| 亚洲成人网在线观看| 国产91在线播放| 亚洲精品网址在线观看| 亚洲在线www| 中文字幕精品一区二区精品| www.久久撸.com| 国产婷婷97碰碰久久人人蜜臀| 黄色91在线观看| 亚洲视屏在线播放| 久久精品中文字幕电影| 亚洲色图国产精品| 久久免费视频网站| 日韩资源在线观看| 国语自产精品视频在线看抢先版图片| 日韩美女免费线视频| 欧美最猛性xxxxx(亚洲精品)| 中文字幕在线亚洲| 国内自拍欧美激情| 国产精品一区二区三| 日韩国产欧美精品在线| 亚洲精品久久久久中文字幕欢迎你| 日韩精品视频免费| 欧美成人免费大片| 欧美日韩中文字幕在线| 亚洲精品美女免费| 91香蕉国产在线观看| 亚洲黄色www网站| 久久精品亚洲国产| 啊v视频在线一区二区三区| 亚洲女人被黑人巨大进入al| 92看片淫黄大片欧美看国产片| 精品高清一区二区三区| 97精品国产aⅴ7777| 色悠悠久久久久| 亚洲欧美色图片| 日韩精品中文字幕在线观看| 中文字幕亚洲欧美| 欧美激情免费视频| 最近日韩中文字幕中文| 色琪琪综合男人的天堂aⅴ视频| 日韩av在线最新| 黄色精品在线看| 久久九九热免费视频| 亚洲一区二区三区乱码aⅴ| 精品久久久久久久久久久久久| 亚洲男女自偷自拍图片另类| 欧美激情乱人伦一区|