ホームページ  >  記事  >  バックエンド開発  >  PHP は GD ライブラリを使用して単純な検証コードを実装します

PHP は GD ライブラリを使用して単純な検証コードを実装します

WBOY
WBOYオリジナル
2016-07-25 08:42:30748ブラウズ
  1. $img=imagecreatetruecolor(100, 40);
  2. $red=imagecolorallocate($img, 255, 0, 0);
  3. $green=imagecolorallocate($img, 0, 255, 0) ;
  4. $blue=imagecolorallocate($img, 0, 0, 255);
  5. $white=imagecolorallocate($img, 255, 255, 255);
  6. $black=imagecolorallocate($img, 0, 0, 0);
  7. //画像を生成
  8. imagefill($img, 0, 0, $black);
  9. //確認コードを設定
  10. $code="";
  11. for($i=0;$i<5;$i++){
  12. $ code.=rand(0,9);
  13. }
  14. //画像に検証コードを書き込みます
  15. imagestring($img, 5, 20, 15, $code, $white);
  16. //少し干渉を追加します
  17. ($ i=0;$i<10;$i++){
  18. imagesetpixel($img, rand(0,100), rand(0,40), $red);
  19. imagesetpixel($img, rand(0,100), rand( 0, 40), $green);
  20. imagesetpixel($img, rand(0,100), rand(0,40), $blue);
  21. }
  22. //干渉を追加します
  23. for($i=0;$i< 1 ;$i++){
  24. imageline($img, rand(0,50), rand(0,20), rand(50,100), rand(20,40), $red);
  25. imageline($img, rand( 0 ,50), rand(0,20), rand(50,100), rand(20,40), $green);
  26. imageline($img, rand(0,50), rand(0,20), rand( 50,100 ), rand(20,40), $blue);
  27. }
  28. header("Content-type:image/png");
  29. imagepng($img);
  30. imagedestroy($img);
  31. ?>
コードをコピー

検証コード、PHP


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