ホームページ  >  記事  >  バックエンド開発  >  PHP画像検証コードの複数の例

PHP画像検証コードの複数の例

WBOY
WBOYオリジナル
2016-07-25 08:52:521090ブラウズ
  1. //ファイルヘッダ...

  2. header("Content-type: image/png")
  3. //トゥルーカラーホワイトペーパーを作成します
  4. $im = @imagecreatetruecolor (50, 20) または die("画像の作成に失敗しました");
  5. //背景色を取得します
  6. $background_color = imagecolorallocate($im, 255, 255, 255); // 背景色を塗りつぶします石油バレルに似ています)
  7. imagefill($im,0,0,$background_color);
  8. //境界線の色を取得します
  9. $border_color = imagecolorallocate($im,200,200,200)
  10. //境界線の色 200,200,200 を描画します
  11. imagerectangle($im,0,0 ,49,19,$border_color);

  12. //背景を 1 行ずつ表示します。全画面表示の場合は 1 または 0 を使用します

  13. for($i= 2;$i / /ランダムな明るい色を取得します
  14. $line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
  15. //線を描画します
  16. imageline($im,2,$i,47,$i, $line_color); }

  17. //フォントサイズを設定します

  18. $font_size=12;

    // 印刷されるテキストを設定します

  19. $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  20. $Str[2] = "01234567891234567890123456"; ;
  21. < ;p> //最初のランダムなテキストを取得します
  22. $imstr[0 ]["s"] = $Str[rand(0,2)][rand(0,25)]; "] = rand(2,5);
  23. $imstr[0 ]["y"] = rand(1,4);

  24. //2 番目のランダムなテキストを取得します

  25. $imstr[ 1]["s"] = $Str[rand(0,2)][rand(0,25)]
  26. $imstr[1]["x"] = $imstr[0]["x"]+ $font_size-1+rand(0,1);
  27. $ imstr[1]["y"] = rand(1,3);

  28. // 3 番目のランダムなテキストを取得します

  29. imstr[2]["s"] = $Str[ rand(0,2)][rand(0,25)]
  30. $imstr[2]["x"] = $imstr[1]["x" ]+$font_size-1+rand(0,1) ;
  31. $imstr[2]["y"] = rand(1,4);

  32. // 4 番目のランダムなテキストを取得します。

  33. $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
  34. $imstr[3]["x"] = $imstr[2][" x"]+$font_size-1+rand(0 ,1);
  35. $imstr[3]["y"] = rand(1,3);

  36. //ランダムな文字列を書き込む

  37. for($i=0;$i //ランダムな暗い色を取得します
  38. $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));
  39. //テキストを描画します
  40. imagechar($im ,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"], $text_color); }> //画像を表示
  41. imagedestroy($im);
  42. コードをコピー
  43. 例 2、美しい PHP 画像検証コードの例 完全なコード:

      /*
    1. * @Author fy
    2. */
    3. $imgwidth =100; //画像の高さ
    4. $codelen =4; //検証コードの長さ
    5. $fontsize =20; //フォントサイズ
    6. $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
    7. $font = 'Fonts/segoesc.ttf';
    8. $im=imagecreatetruecolor($imgwidth,$imgheight);
    9. $while=imageColorAllocate($im,255) ,25 5,255 );
    10. imagefill($im,0,0,$while); //画像を塗りつぶします
    11. //文字列を取得します
    12. $authstr='';
    13. $_len = strlen($charset)-1;
    14. for ($i =0;$i $authstr .= $charset[mt_rand(0,$_len)];
    15. }
    16. session_start();
    17. $_SESSION['scode']= strto lower($authstr );//主に大文字と小文字の区別を避けるために、すべてを小文字に変換します
    18. //星に変更されたランダムなドットを描画します
    19. for ($i=0;$i<$imgwidth;$i++){
    20. $randcolor =imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
    21. imagestring($im,mt_rand(1,5), mt_rand(0,$imgwidth),mt_rand(0,$ imgheight) , '*',$randcolor);
    22. //imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
    23. }
    24. //ランダムに線を描画します。行数 = 文字数 (任意)
    25. for($i=0;$i<$codelen;$i++)
    26. {
    27. $randcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)) ;
    28. imageline ($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$randcolor);
    29. }
    30. $_x=intval($imgwidth/$codelen);文字距離
    31. $_y=intval($imgheight*0.7); //文字は画像の 70% に表示されます
    32. for($i=0;$i $randcolor= imagecolorallocate($im ,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
    33. //imagestring($im,5,$j,5,$imgstr[$i],$color3);
    34. // imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
    35. imagettftext($im,$fontsize,mt_rand(-30,30) ,$i* $_x+3,$_y,$randcolor,$font,$authstr[$i]);
    36. }
    37. //画像を生成
    38. header("content-type:image/PNG");
    39. imagePNG( $im);
    40. imageDestroy($im);
    41. コードをコピー

    例3、php5画像検証コードのサンプルコード

    画像検証コードを生成するphp5の例。

    PHP GD ライブラリ関数を使用する必要があります: 1,imagecreatetruecolor-----トゥルー カラー イメージを作成します imagecreatetruecolor(int x_size,int y_size) //x は幅を表し、y は高さを表します 2. imagecolorallocate は画像に色 (パレット) を割り当てます。 imagecolorallocate(リソース画像,int red,int green,int blue)//赤、緑、青----三原色 3. 画像文字列描画機能 iamgestring(リソース画像,フォント,int x,int y,コンテンツ,カラー); 4. 出力機能 PHP のヘッダーは、PHP5 がサポートするヘッダーを定義するアクションです。 1、コンテンツタイプ: xxxx/yyyy 2、場所:xxxx:yyyy/zzzz 3、ステータス: nnn xxxxxx xxxx/yyyy はコンテンツ ファイルの種類を示します 例: 画像/GIF 画像/jpeg 画像/png 例: header("Content-type:image/jpeg") GD ライブラリには対応する画像タイプがあります imagejpeg()、imagegif()、imagepang() 5.イメージライン線画機能 iamgeline(リソース画像,int x1,int y1,int x2,int y2,int color); 画像 ---画像 x1 ---開始座標 y1 x2 ---終点座標 y2 6.imagesetpixel描画ポイント関数 imagesetpixel(リソース画像,int x,int y,int color) 7.imagettfフォントによるテキスト書き込み機能 imagettftext(リソース画像,floatサイズ,float角度,int x,int y,int color,string fontfile,string text) 8.中国語のphp検証コードを挿入する方法 iconv("gb2312","utf-8","string"); //まずテキストを utf-8 形式に変換します。 9. ランダム機能 1, rand([int min,int max]) //rand(1,4) は 1 から 4 までの数値を生成します 2, dechex (10 進数) //16 進数に変換します

    確認コードを生成する手順: 乱数を生成 -- 画像を作成 -- 乱数を画像に書き込み -- セッションに保存

    確認コードの入力例 gdchek.php

    1. /*
    2. * イメージ検証コード
    3. * を生成し、エディターでテンプレートを開きます
    4. */
    5. session_start(); $i++){
    6. $rand.=dechex(rand(1,15)); //16 進数を含む 4 桁の乱数を生成します
    7. }
    8. $_SESSION[check_gd]=$rand;
    9. $img=imagecreatetruecolor; 30); //画像を作成します
    10. $bg=imagecolorallocate($img,0,0,0) //初回の背景色生成
    11. $fc=imagecolorallocate($img,255,255,255); color
    12. //絵に線を描く
    13. for($i=0;$i<3;$i++){
    14. $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255 )) ;
    15. imageline($img,rand(0,15),0,100,30,$te);
    16. //画像上にいくつかの点を描画します
    17. for($i=0;$i<200;$i++){
    18. $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
    19. imagesetpixel($img,rand()%100,rand()%30,$te); //まずテキストをutf-8形式に変換します
    20. //$str=iconv("gb2312","utf-8","Hehehe");
    21. //中国語検証を追加します
    22. //smkai.ttfはAフォントですファイルを他の人のコンピュータでもフォントとして機能させるには、そのファイルをプロジェクトのルート ディレクトリに置き、
    23. imagettftext($img,11,10,20, 20,$fc,") もあります。 simkai.ttf","Hello Hello");
    24. //画像に文字列を書き込む
    25. //imagestring($img,rand(1,6),rand(3,70) ,rand(3,16),$ rand,$fc);
    26. //出力画像
    27. header("Content-type:image/jpeg")
    28. ?>
    29. コードをコピー
    login.php

    /*
    *
      *
    1. */
    2. session_start();
    3. if($_POST[sub]){
    4. //認証コードが同じか判定
    5. if($ _POST[gd_pic ]==$_SESSION[check_gd]){
    6. echo "検証が成功しました!";
    7. }else{
    8. echo "検証コード エラー"
    9. }
    10. }
    11. ユーザー名:
    12. パスワード:
    13. 確認コード:
    14. コードをコピー
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。