亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

驗證碼(數字,字母,數字字母混合,中文)

<?php
/*********************
*????securityCode.class.php
*????驗證碼(數字,字母,數字字母混合,中文)
********************/
class?SecurityCode{
????private?$img_width;
????private?$img_height;
????private?$mode;//1全部數字,2全部字母,3數字字母混合,4全部中文
????private?$text="的一是不了能好都然沒日于起還發成事只作當想看文無開手十用主行方又如前所本見經頭面公同三已老從動兩長知民樣現分將外但身些與高意進把法此實回二理美點月明其種聲全工己話兒者向情部正名定女問力機給等幾很業最間新什打便位因重被走電四第門相次東政??谑菇涛髟倨秸媛犑罋庑疟鄙訇P并內加化由卻代軍產入先山五太水萬市眼體別處總才場師書比住員九笑性通目華報立馬命張活難神數件安表原車白應路期叫死常提感金何更反合放做系計或司利受光王果親界及今京務制解各任至清物臺象記邊共風戰干接它許八特覺望直服毛林題建南度統色字請交愛讓";

????private?$imageResource;//資源

????public?function?__construct($width,$height,$mode){
????????$this->init($width,$height,$mode);
????}
????
????/*
????*????@function?初始化參數
????*????@param?$width圖片寬度?$height圖片高度?$驗證碼的模式
????*/
????public?function?init($width,$height,$mode){
????????$this->img_width=$width;
????????$this->img_height=$height;
????????$this->mode=$mode;
????}

????public?function?getSecurityCodeImg(){
????????header("content-type:image/png");
????????$this->imageResource=imagecreatetruecolor($this->img_width,$this->img_height);
????????$white=imagecolorallocate($this->imageResource,255,255,255);
????????imagefill($this->imageResource,0,0,$white);
????????//寫內容
????????$this->setContent();
????????//干擾點
????????$this->setPixel();
????????//隨機線
????????$this->setLine();
????????//橢圓線
????????$this->setArc();
????????//以png數據格式繪制圖片并輸出
????????imagepng($this->imageResource);
????????//銷毀資源
????????imagedestroy($this->imageResource);
????}

????/*
????*????@function?寫內容
????*/
????public?function?setContent(){????????
????????$codeArr=$this->getCode();
????????//開啟session
????????session_start();
????????$_SESSION["SecurityCode"]=implode("",$codeArr);
????????foreach($codeArr?as?$key=>$value){
????????????$rantcolor=imagecolorallocate($this->imageResource,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));//因為0-120為深色
????????????$fontsize=mt_rand(13,15);
????????????$angle=mt_rand(-10,10);
????????????$x=$key*intval($this->img_width/4)+mt_rand($fontsize,$fontsize*2);
????????????$y=mt_rand(25,50);
????????????$fontfile="./msyh.ttc";//字體所在的文件路徑名
????????????imagefttext($this->imageResource,$fontsize,$angle,$x,$y,$rantcolor,$fontfile,$value);
????????}
????}
????
????/*
????*????@function?設定干擾元素
????*/
????public?function?setPixel(){
????????$black=imagecolorallocate($this->imageResource,0,0,0);
????????for($i=0;$i<200;$i++){
????????????imagesetpixel($this->imageResource,mt_rand(0,$this->img_width),mt_rand(0,$this->img_height),$black);
????????}
????}
????
????/*
????*????@function?設定干擾線
????*/
????public?function?setLine(){
????????$black=imagecolorallocate($this->imageResource,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
????????imageline($this->imageResource,mt_rand(0,$this->img_width),mt_rand(0,$this->img_height),mt_rand(0,$this->img_width),mt_rand(0,$this->img_height),$black);
????}

????/*
????*????@function?設定干擾橢圓線
????*/
????public?function?setArc(){
????????$white=imagecolorallocate($this->imageResource,0,0,0);
????????$cx=mt_rand(0,$this->img_width);
????????$cy=mt_rand(0,$this->img_height);
????????$w=mt_rand(0,$this->img_width);
????????$h=mt_rand(0,$this->img_height);
????????imagearc($this->imageResource,$cx,$cy,$w,$h,?0,?360,?$white);
????}

????/*
????*????@function?獲得驗證碼數組
????*????@return?array
????*/
????public?function?getCode(){
????????$tmp_arr=array();
????????if($this->mode==1){
????????????//全部數字的
????????????$tmp_arr=$this->getNumericCode();
????????}
????????else?if($this->mode==2){
????????????//全部字母
????????????$tmp_arr=$this->getEnCode();
????????}
????????else?if($this->mode==3){
????????????//數字與字母混合
????????????$tmp_arr=$this->getMixedCode();
????????}
????????else?if($this->mode==4){
????????????//全部中文
????????????$tmp_arr=$this->getChinaCode();
????????}
????????return?$tmp_arr;
????}
????
????/*
????*????@function?獲得全數字數組
????*/
????public?function?getNumericCode(){
????????$codeArr=array();
????????for($i=0;$i<4;$i++){
????????????$codeArr[]=mt_rand(0,9);
????????}
????????return?$codeArr;
????}
????
????/*
????*????@function?獲得全字母數組
????*/
????public?function?getEnCode(){
????????$codeArr=array();
????????for($i=0;$i<4;$i++){
????????????$rant=mt_rand(1,2);
????????????if($rant==1){
????????????????$codeArr[]=chr(mt_rand(65,90));
????????????}
????????????else{
????????????????$codeArr[]=chr(mt_rand(97,122));
????????????}
????????}
????????return?$codeArr;
????}
????
????/*
????*????@function?獲得字母與數字的混合數組
????*/
????public?function?getMixedCode(){
????????$codeArr=array();
????????for($i=0;$i<4;$i++){
????????????$rant=mt_rand(1,3);
????????????if($rant==1){
????????????????$codeArr[]=mt_rand(0,9);
????????????}
????????????else?if($rant==2){
????????????????$codeArr[]=chr(mt_rand(65,90));
????????????}
????????????else{
????????????????$codeArr[]=chr(mt_rand(97,122));
????????????}
????????}
????????return?$codeArr;
????}
????
????/*
????*????@function?獲得全中文數組
????*/
????public?function?getChinaCode(){
????????//header("content-type:text/html;charset=utf-8");
????????$str_arr=str_split($this->text,3);//因為utf8里中文占3個字節
????????$codeArr=array();
????????for($i=0;$i<4;$i++){
????????????$rant=mt_rand(0,count($str_arr)-1);
????????????$codeArr[]=$str_arr[$rant];
????????}
????????return?$codeArr;
????}
}
$sc=new?SecurityCode(200,60,4);
$sc->getSecurityCodeImg();
?>

不足的地方是,無法將橢圓圓弧設定在一個范圍內,衍生出的是字符集的不同ASCII、GBK、Unicode、UTF8的使用環境。

正在回答

0 回答

舉報

0/150
提交
取消
PHP實現驗證碼制作
  • 參與學習       37928    人
  • 解答問題       338    個

各種形態驗證碼核心原理與實現技巧,講解實現過程中的技術難點

進入課程

驗證碼(數字,字母,數字字母混合,中文)

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號