最近因項目的要求,需要使用漢字驗證碼,於是研究了一個,在這裡貼出代碼來和大家分享一下。下面是使用php產生漢字驗證碼的具體用法和函數代碼。
用法如下:
<?phpcreate_excode(4);//產生四個漢字的驗證碼
漢字驗證碼圖片:
具體函數代碼如下:
<?php/** $length 驗證碼漢字個數*/function create_excode($length){$randChar=array("浩","比","不","驚","靜","看","友","前","花","開","龍","落","義","得","江","無","意","虎","望","天","外","雲","卷","市","丁","中","程","人","產","名","僅","餘","金","國","美","幣","東","木","水","火","土","七","九","八","工","碼","圖","員","電","大","秒","舒","仁");header("content-type: image/png");$charWidth=30;//每個字元佔有的寬度$image_x=$length*$charWidth; //圖片寬度$image_y=40; //圖片高度$noise_num=100*$length; //雜點數量$line_num=13; //幹擾線數量$image=imagecreate($image_x,$image_y);$w=$h=0;//圖片款高度初始化imagecolorallocate($image,250,250,250); //設定圖片背景顏色//imagecolorallocate($image,0xff,0xff,0xff);//白色背景$rectangle_color=imagecolorallocate($image,0xAA,0xAA,0xAA); //邊框顏色$noise_color=imagecolorallocate($image,0x00,0x00,0x00); //雜點顏色$font_size=18;//字型大小$font_y=29;//字元在Y軸上基準的位置$font_face="heiti.ttf"; //字型//加入雜點for($i=0;$i<$noise_num;$i++){imagesetpixel($image,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);}//產生驗證碼$x=2;$session_code="";for($i=0;$i<$length;$i++){$font_color=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //字型顏色$code=$randChar[mt_rand(0,count($randChar)-1)];imagettftext($image,$font_size,mt_rand(-6,6),$x,$font_y,$font_color,$font_face,$code);$x+=30;$session_code.=$code;}//把驗證碼的值存放到session中@session_start();$_SESSION["checkCode"]=$session_code;for($i=0;$i<$line_num;$i++){ $fontcolor=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));imagearc($image,mt_rand(-10,$w),mt_rand(-10,$h),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor); }//www.Alixixi.com/php-function/1010.htmlfor($i=0;$i<255;$i++){ $fontcolor=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));imagesetpixel($image,mt_rand(0,$w),mt_rand(0,$h),$fontcolor); }imagerectangle($image,0,0,$image_x-1,$image_y-1,$rectangle_color); //加個邊框imagepng($image);imagedestroy($image);}
漢字驗證碼代碼下載(內含字型檔)
您可能感興趣的文章
- php提取社會安全號碼碼中的生日日期以及驗證是否為未成年人的函數
- php產生動態驗證碼圖片(gif)
- php表單欄位格式驗證類
- php產生驗證碼函數
- php擷取漢字拼音首字母的函數(真正可以使用的)
- PHP將簡體漢字轉為繁體的方法
- js,php正則驗證是否為數字與字母的混合(6-15位)
- thinkphp自動驗證與自動填滿無效的解決辦法