ホームページ  >  記事  >  バックエンド開発  >  PHP検証コードのラッパークラス

PHP検証コードのラッパークラス

WBOY
WBOYオリジナル
2016-07-25 08:58:47964ブラウズ
  1. /**

  2. * 認証コードクラス
  3. * bbs.it-home.org を編集
  4. */
  5. class ValidationCode {
  6. private $width;
  7. private $height;
  8. private $codeNum;
  9. private $image; //image;リソース
  10. private $disturbColorNum;
  11. private $checkCode;
  12. function __construct($width=80, $height=20, $codeNum=4){
  13. $this->width=$width;
  14. $this->height= $height;
  15. $this->codeNum=$codeNum;
  16. $this->checkCode=$this->createCheckCode();
  17. $number=floor($width*$height/15);
  18. if($number > 240-$codeNum){

  19. $this->disturbColorNum= 240-$codeNum;
  20. }else{
  21. $this->disturbColorNum=$number;
  22. }< /p>
  23. }

  24. //このメソッドにアクセスして画像をブラウザに出力します
  25. function showImage($fontFace=""){
  26. //ステップ 1: 画像の背景を作成します
  27. $this->createImage ();
  28. //ステップ 2: 干渉要素を設定します
  29. $this->setDisturbColor();
  30. //ステップ 3: 画像にランダムなテキストを描画します
  31. $this->outputText($fontFace);
  32. //ステップ 4: 出力画像
  33. $this->outputImage();
  34. }

  35. //このメソッドを呼び出して、ランダムに作成された検証コード文字列を取得します

  36. function getCheckCode(){
  37. return $this ->checkCode;
  38. }
  39. プライベート関数 createImage(){
  40. //画像リソースを作成//bbs.it-home.org
  41. $this->image=imagecreatetruecolor($this->width, $this-> ;height);
  42. //ランダムな背景色
  43. $backColor=imagecolorallocate($this->image, rand(225, 255), rand(225,255), rand(225, 255));
  44. // に色を追加します。背景
  45. imagefill($this->image, 0, 0, $backColor);
  46. //境界線の色を設定します
  47. $border=imagecolorallocate($this->image, 0, 0, 0);
  48. //描画長方形の境界線
  49. imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
  50. }
  51. プライベート関数 setDisturbColor() {
  52. for ($i=0; $idisturbColorNum; $i++){
  53. $color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0) , 255));
  54. imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color); for($i=0; $i$color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255)) ;
  55. imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20 , 200) , 55, 44, $color);
  56. }
  57. }
  58. private function createCheckCode(){
  59. //ランダム コードは主にここで生成され、1 と l
  60. $code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
  61. $string を区別するために 2 から始まります。 ='';
  62. for($i=0; $i < $this->codeNum; $i++){
  63. $char=$code{rand(0, strlen($code)-1)}; string.=$char;
  64. }
  65. return $string;
  66. }
  67. プライベート関数outputText($fontFace=""){
  68. for($i=0; $i<$this->codeNum; $i++) {
  69. $fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
  70. if($fontFace==""){
  71. $fontsize =rand (3, 5);
  72. $x=floor($this->width/$this->codeNum)*$i+3;
  73. $y=rand(0, $this->height-15 );
  74. imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
  75. }else{
  76. $fontsize=rand(12, 16) ;
  77. $x=floor(($this->width-8)/$this->codeNum)*$i+8;
  78. $y=rand($fontSize+5, $this->height); ($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
  79. }
  80. }
  81. }< ;/p>
  82. //画像情報を出力

  83. プライベート関数outputImage() {
  84. if(imagetypes() & IMG_GIF){
  85. header("Content-Type:image/gif");
  86. imagepng ($this ->image);
  87. }else if(imagetypes() & IMG_JPG){
  88. header("Content-Type:image/jpeg");
  89. imagepng($this->image); //bbs. .org
  90. }else if(imagetypes() & IMG_PNG){
  91. header("Content-Type:image/png");
  92. imagepng($this->image);
  93. }else if(imagetypes() & IMG_WBMP) {
  94. header("Content-Type:image/vnd.wap.wbmp");
  95. imagepng($this->image);
  96. }else{
  97. die("PHP はイメージの作成をサポートしていません");
  98. }
  99. }
  100. function __destruct(){
  101. imagedestroy($this->image);
  102. }
  103. }
  104. ?>

  105. コードをコピー
検証コードクラスを呼び出す例:

session_start();
include "validationcode.class.php";
    $code=new ValidationCode(80, 20, 4);
  1. $code->showImage(); /登録またはログイン用のページに出力します
  2. $_SESSION["code"]=$code->getCheckCode() //認証コードをサーバーに保存します
  3. ?>
  4. ;コードをコピー


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