驗(yàn)證碼(數(shù)字,字母,數(shù)字字母混合,中文)
<?php /********************* *????securityCode.class.php *????驗(yàn)證碼(數(shù)字,字母,數(shù)字字母混合,中文) ********************/ class?SecurityCode{ ????private?$img_width; ????private?$img_height; ????private?$mode;//1全部數(shù)字,2全部字母,3數(shù)字字母混合,4全部中文 ????private?$text="的一是不了能好都然沒日于起還發(fā)成事只作當(dāng)想看文無開手十用主行方又如前所本見經(jīng)頭面公同三已老從動(dòng)兩長知民樣現(xiàn)分將外但身些與高意進(jìn)把法此實(shí)回二理美點(diǎn)月明其種聲全工己話兒者向情部正名定女問力機(jī)給等幾很業(yè)最間新什打便位因重被走電四第門相次東政??谑菇涛髟倨秸媛犑罋庑疟鄙訇P(guān)并內(nèi)加化由卻代軍產(chǎn)入先山五太水萬市眼體別處總才場師書比住員九笑性通目華報(bào)立馬命張活難神數(shù)件安表原車白應(yīng)路期叫死常提感金何更反合放做系計(jì)或司利受光王果親界及今京務(wù)制解各任至清物臺象記邊共風(fēng)戰(zhàn)干接它許八特覺望直服毛林題建南度統(tǒng)色字請交愛讓"; ????private?$imageResource;//資源 ????public?function?__construct($width,$height,$mode){ ????????$this->init($width,$height,$mode); ????} ???? ????/* ????*????@function?初始化參數(shù) ????*????@param?$width圖片寬度?$height圖片高度?$驗(yàn)證碼的模式 ????*/ ????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); ????????//寫內(nèi)容 ????????$this->setContent(); ????????//干擾點(diǎn) ????????$this->setPixel(); ????????//隨機(jī)線 ????????$this->setLine(); ????????//橢圓線 ????????$this->setArc(); ????????//以png數(shù)據(jù)格式繪制圖片并輸出 ????????imagepng($this->imageResource); ????????//銷毀資源 ????????imagedestroy($this->imageResource); ????} ????/* ????*????@function?寫內(nèi)容 ????*/ ????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));//因?yàn)?-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?設(shè)定干擾元素 ????*/ ????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?設(shè)定干擾線 ????*/ ????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?設(shè)定干擾橢圓線 ????*/ ????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?獲得驗(yàn)證碼數(shù)組 ????*????@return?array ????*/ ????public?function?getCode(){ ????????$tmp_arr=array(); ????????if($this->mode==1){ ????????????//全部數(shù)字的 ????????????$tmp_arr=$this->getNumericCode(); ????????} ????????else?if($this->mode==2){ ????????????//全部字母 ????????????$tmp_arr=$this->getEnCode(); ????????} ????????else?if($this->mode==3){ ????????????//數(shù)字與字母混合 ????????????$tmp_arr=$this->getMixedCode(); ????????} ????????else?if($this->mode==4){ ????????????//全部中文 ????????????$tmp_arr=$this->getChinaCode(); ????????} ????????return?$tmp_arr; ????} ???? ????/* ????*????@function?獲得全數(shù)字?jǐn)?shù)組 ????*/ ????public?function?getNumericCode(){ ????????$codeArr=array(); ????????for($i=0;$i<4;$i++){ ????????????$codeArr[]=mt_rand(0,9); ????????} ????????return?$codeArr; ????} ???? ????/* ????*????@function?獲得全字母數(shù)組 ????*/ ????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?獲得字母與數(shù)字的混合數(shù)組 ????*/ ????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?獲得全中文數(shù)組 ????*/ ????public?function?getChinaCode(){ ????????//header("content-type:text/html;charset=utf-8"); ????????$str_arr=str_split($this->text,3);//因?yàn)閡tf8里中文占3個(gè)字節(jié) ????????$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(); ?>
不足的地方是,無法將橢圓圓弧設(shè)定在一個(gè)范圍內(nèi),衍生出的是字符集的不同ASCII、GBK、Unicode、UTF8的使用環(huán)境。