php生成驗證碼 支持中文 字母 數字混合

wp5x 9年前發布 | 2K 次閱讀 PHP

<?php
 //驗證碼的制作
 //1.生成4位的隨機數
 for($i=0;$i<4;$i++){
     $rand.=dechex(rand(1, 15));
 }

//2.創建圖片 //創建一張圖片 $image=imagecreatetruecolor(100, 30); //背景顏色 $bg=imagecolorallocate($image, 0, 0, 0); $te=imagecolorallocate($image, 255,255,255);

//劃線 for($i=0;$i<4;$i++){ $lineColor=imagecolorallocate($image, rand(20, 225), rand(20, 225), rand(20, 225)); imageline($image, rand(0, 100), 0, 100,30, $lineColor); } //劃點 for($i=0;$i<200;$i++){ imagesetpixel($image, rand()%100, rand()%100, $lineColor); } //添加文字 //編碼轉換 $str=iconv("GBK", "UTF-8", "廣州GZ"); imagettftext($image, 12,11, 20, 20, $lineColor, 'simhei.ttf', $str);

//3.隨機數寫入圖片$font=(1~6) imagestring($image, rand(3, 6), rand(3, 70), rand(0, 16), $rand, $te);

//4.隨機數保存在session中 //啟動session session_start(); //把驗證碼放入session $_SESSION[vilidationCode]=$rand;

header("Content-type: image/jpeg"); imagejpeg($image); ?></pre>

 本文由用戶 wp5x 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!