服务器之家

服务器之家 > 正文

非常实用的php验证码类

时间:2021-01-18 14:19     来源/作者:LoeYueng

本文实例为大家分享了php验证码类,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
 *
 * @author Administrator
 *
 */
class ValidateCode{
   
  private $width;
  private $height;
  private $codeNum;
  private $img_resouce;
  private $disturbColorNum;
  private $checkCode;
   
  function __construct($width=80,$height=20,$codeNum=4) {
    $this->width=$width;
    $this->height=$height;
    $this->codeNum=$codeNum;
    $this->checkCode=$this->CreateCheckCode();
    $number=floor($width*$height/25);
    if ($number>240-$codeNum) {
      $this->disturbColorNum=240-$codeNum;
    }else{
      $this->disturbColorNum=$number;
    }
  }
   
  public function showImage($fontpath='') {
    //创建图像背景
    $this->Img_resouce();
    //var_dump($img_resouce);
    //设置干扰元素
    $this->setDistructcolor();
    //向图像中随机画出文本
    $this->outputtext($fontpath);
    //输出图像
    $this->outputimage();
  }
  /**
   *
   *获取随机创建的验证码
   */
  public function getCheckCode(){
     
  }
  private function Img_resouce(){
    //创建一个真彩图像
    $this->img_resouce=imagecreatetruecolor($this->width,$this->height);
    //随机设置图像背景
    $backcolor=imagecolorallocate($this->img_resouce,rand(225,255),rand(225,255),rand(225,255));
    //填充颜色
    imagefill($this->img_resouce, 0, 0, $backcolor);
    //设置边框背景
    $border=imagecolorallocate($this->img_resouce, 0,0,0);
    //画一个矩形
    imagerectangle($this->img_resouce,0,0,$this->width-1,$this->height-1,$border);
  }
  private function setDistructcolor(){
    //绘画干扰点
    for ($i = 0; $i <$this->disturbColorNum; $i++) {
       
      imagesetpixel($this->img_resouce, rand(1, $this->width-2), rand(1, $this->height-2), rand(0,255));
    }
     
    //绘画干扰线
    for ($j = 0; $j <3; $j++) {
      $linecolor=imagecolorallocate($this->img_resouce,rand(0,255),rand(0,255),rand(0,255));
      imagearc($this->img_resouce, rand(0,$this->width), rand(0,$this->height),
       rand(10, 225), rand(20, 150),
       55, 44, $linecolor);
    }
  }
  private function CreateCheckCode(){
    $code='23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ';
    $string='';
    for ($i = 0; $i < $this->codeNum; $i++) {
       
      $char=$code{rand(0, strlen($code)-1)};
      $string.=$char;
    }
    return $string;
  }
  private function outputtext($fontpath=''){
    for ($i = 0; $i < $this->codeNum; $i++) {
      $fontcolor=imagecolorallocate($this->img_resouce, rand(0,128), rand(0, 128), rand(0, 128));
      if ($fontpath=='') {
         
         $fontsize=rand(3, 5);
         $x=floor($this->width/$this->codeNum)*$i+3;
         $y=rand(0, $this->height-20);
         imagechar($this->img_resouce, $fontsize, $x, $y, $this->checkCode{$i}, $fontcolor);
    }else{
         $fontsize=rand(12, 16);
         $x=floor(($this->width-8)/$this->codeNum)*$i+8;
         $y=rand($fontsize, $this->height-15);
         imagettftext($this->img_resouce,$fontsize,rand(-45,45),$x,$y,$fontcolor,fontpath,$this->checkCode{$i});
       }
    }
  }
  private function outputimage() {
     
    if (imagetypes() & IMG_GIF) {
      header("Content-type: image/gif");
      imagegif($this->img_resouce);
    }else if(imagetypes() & IMG_JPEG) {
      header("Content-type: image/jpeg");
      imagejpeg($this->img_resouce);
    }else if(imagetypes() & IMG_PNG) {
      header("Content-type: image/png");
      imagepng($this->img_resouce);
    }else {
      echo "PHP不支持的类型";
    }
     
     
  }
  private function __destruct(){
     
    imagedestroy($this->img_resouce);
  }
}
?>

以上就是本文的全部内容,希望对大家的学习有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部