一個好用的驗證碼PHP工具類

cp5m 9年前發布 | 1K 次閱讀 PHP

 
<?php
/**

  • 驗證碼類 */ class Base_Tool_Verify{ /**

    • 生成驗證碼方法 */ public static function createCode(){ //生成驗證碼圖片 // 全數字 $str = "D,B,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,W,J,K,M,M,N,K,P,Q,R,S,T,U,V,W,X,Y,Z"; //要顯示的字符,可自己進行增刪 $list = explode(",", $str); $cmax = count($list) - 1; $verifyCode = ''; for ( $i=0; $i < 4; $i++ ){

       $randnum = mt_rand(0, $cmax);
       $verifyCode .= $list[$randnum];           //取出字符,組合成為我們要的驗證碼字符
      

      } $_SESSION['code'] = $verifyCode; //將字符放入SESSION中

      $im = imagecreate(80,28); //生成圖片 $black = imagecolorallocate($im, 0 ,0 ,0); //此條及以下三條為設置的顏色 $white = imagecolorallocate($im, 244,249,244); $gray = imagecolorallocate($im, rand(200,255),rand(200,255),rand(200,255)); $red = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255)); $rand_color = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255)); $rand_color2 = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255)); imagefill($im,0,0,$white); //給圖片填充顏色 $pix=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); mt_srand(); for($i=0;$i<100;$i++) { imagesetpixel($im,mt_rand(0,180),mt_rand(0,35),$pix);
      } imageline($im, 0,rand(0,28), 80, rand(0,28), $rand_color);
      imageline($im, 0,rand(0,28), 80, rand(0,28), $rand_color2);

      //將驗證碼繪入圖片 $mycolor = imagecolorallocate($im, 0, 78, 152); $path = API_PATH.DS.'Base'.DS.'Tool'; putenv('GDFONTPATH=' . $path); $font = 'simhei.ttf'; $arrstr = str_split($verifyCode); imagettftext($im, 20, rand(0,50), 10, rand(20,30), $mycolor, $font, $arrstr[0]); imagettftext($im, 15, rand(0,50), 20, rand(20,30), $mycolor, $font, $arrstr[1]); imagettftext($im, 15, rand(0,50), 30, rand(20,30), $mycolor, $font, $arrstr[2]); imagettftext($im, 15, rand(0,50), 40, rand(20,30), $mycolor, $font, $arrstr[3]); $font2=imagecolorallocate($im,41,163,238); imagerectangle($im,0,0,1,1,$font2); imagepng($im); imagedestroy($im); } } </pre>

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