/** Description: function function is to generate verification code * Parameter Description: Input length, width, height*/function Vcode ($_code_length=4, $_width = the, $_height = -) {$_font=dirname (__file__).'/font/elephant.ttf';//Note that the font path must be written, otherwise it will not show the picture$_fontsize=$_height/1.8; $_nmsg=""; for($i =0; $i <$_code_length; $i + +) {$_nmsg.= Dechex (Mt_rand (0, the)); } if(!isset ($_session)) {session_start (); } $_session["Vcode"] =$_nmsg; $_img=Imagecreatetruecolor ($_width, $_height); $_white= Imagecolorallocate ($_img, -, -, -); Imagefill ($_img,0,0, $_white); $_gray= Imagecolorallocate ($_img,196,196,196); Imagerectangle ($_img,0,0, $_width-1, $_height-1, $_gray); for($i =0; $i <6; $i + +) {$_md_color= Imagecolorallocate ($_img, Mt_rand ( $,255), Mt_rand ( $,255), Mt_rand ( $,255)); Imageline ($_img, Mt_rand (0, $_width), Mt_rand (0, $_height), Mt_rand (0, $_width), Mt_rand (0, $_height), $_md_color); } for($i =0; $i < -; $i + +) {$_md_color= Imagecolorallocate ($_img, Mt_rand ( $,255), Mt_rand ( $,255), Mt_rand ( $,255)); Imagestring ($_img,1, Mt_rand (1, $_width-5), Mt_rand (1, $_height-5),"*", $_md_color); } for($i =0; $i < $_code_length; $i + +) {$_md_color= Imagecolorallocate ($_img, Mt_rand (0,102), Mt_rand (0,102), Mt_rand (0,102)); //imagestring ($_img, 5, $i * $_width/$_code_length+ mt_rand (1, ten), Mt_rand (1, $_HEIGHT/2), $_session["Vcode" [$i], $_ Md_color);Imagettftext ($_img,$_fontsize,mt_rand (- -, -), $_fontsize* $i +mt_rand (1,5), $_height/1.4, $_md_color,$_font,$_session["Vcode"[$i]); } Header ("Content-type:image/png"); Imagepng ($_IMG); Imagedestroy ($_IMG); }
Need a font file can baidu download a
Simple and beautiful PHP verification code function