ホームページ  >  記事  >  バックエンド開発  >  非常に便利な検証コード ツール クラス

非常に便利な検証コード ツール クラス

WBOY
WBOYオリジナル
2016-07-25 08:47:391074ブラウズ
  1. /**
  2. * 認証コードクラス
  3. */
  4. class Base_Tool_Verify{
  5. /**
  6. * 認証コードの生成方法
  7. */
  8. public static function createCode(){
  9. //検証コード画像を生成
  10. // すべての数字
  11. $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"; //表示する文字は自分で追加・削除可能
  12. $list =explode(", ", $ str);
  13. $cmax = count($list) - 1;
  14. $verifyCode = '';
  15. for ( $i=0; $i $randnum = mt_rand(0) , $cmax );
  16. $verifyCode .= $list[$randnum]; //文字を取り出して、必要な検証コード文字に結合します
  17. }
  18. $_SESSION['code'] = $verifyCode; SESSIONに文字を入れる
  19. $im = imagecreate(80,28); //画像を生成する
  20. $black = imagecolorallocate($im, 0,0,0) //これと次の3つの項目が設定された色です
  21. $white = imagecolorallocate($im , 244,249,244);
  22. $gray = imagecolorallocate($im, rand(200,255),rand(200,255),rand(200,255));
  23. $red = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));
  24. $rand_color = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
  25. $rand_color2 = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
  26. imagefill($im,0,0,$white); //画像を色で塗りつぶします
  27. $pix=imagecolorallocate($im,rand(0,255),rand(0,255) ,rand(0,255));
  28. mt_srand();
  29. for($i=0;$i {
  30. imagesetpixel($im,mt_rand(0,180),mt_rand(0,35),$pix );
  31. }
  32. イメージライン($ im, 0,rand(0,28), 80, rand(0,28), $rand_color);
  33. イメージライン($im, 0,rand(0,28), 80, rand (0,28), $rand_color2 );
  34. //画像に確認コードを描画します
  35. $mycolor = imagecolorallocate($im, 0, 78, 152);
  36. $path = API_PATH.DS.'Base'.DS .'ツール';
  37. putenv(' GDFONTPATH=' . $path);
  38. $font = 'simhei.ttf';
  39. $arrstr = str_split($verifyCode);
  40. imagettftext($im, 20, rand(0,50) ), 10, rand(20,30 ), $mycolor, $font, $arrstr[0]);
  41. imagettftext($im, 15, rand(0,50), 20, rand(20,30), $mycolor , $font, $arrstr[1] );
  42. imagettftext($im, 15, rand(0,50), 30, rand(20,30), $mycolor, $font, $arrstr[2]);
  43. imagettftext ($im, 15, rand(0 ,50), 40, rand(20,30), $mycolor, $font, $arrstr[3]);
  44. $font2=imagecolorallocate($im,41,163,238);
  45. imagerectangle( $im,0,0,1, 1,$font2);
  46. imagepng($im);
  47. imagedestroy($im);
  48. }
  49. }
コードをコピー

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。