一个不错的php验证码的类

类的代码:

 1 <?php
 2 
 3 class Captcha
 4 {
 5     private $width;
 6     private $height;
 7     private $codeNum;
 8     private $code;
 9     private $im;
10 
11     function __construct($width=80, $height=20, $codeNum=4)
12     {
13         $this->width = $width;
14         $this->height = $height;
15         $this->codeNum = $codeNum;
16     }
17 
18     function showImg()
19     {
20         //创建图片
21         $this->createImg();
22         //设置干扰元素
23         $this->setDisturb();
24         //设置验证码
25         $this->setCaptcha();
26         //输出图片
27         $this->outputImg();
28     }
29 //http://www.cnblogs.com/sosoft/
30     function getCaptcha()
31     {
32         return $this->code;
33     }
34 
35     private function createImg()
36     {
37         $this->im = imagecreatetruecolor($this->width, $this->height);
38         $bgColor = imagecolorallocate($this->im, 0, 0, 0);
39         imagefill($this->im, 0, 0, $bgColor);
40     }
41 
42     private function setDisturb()
43     {
44         $area = ($this->width * $this->height) / 20;
45         $disturbNum = ($area > 250) ? 250 : $area;
46         //加入点干扰
47         for ($i = 0; $i < $disturbNum; $i++) {
48             $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
49             imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
50         }
51         //加入弧线
52         for ($i = 0; $i <= 5; $i++) {
53             $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
54             imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
55         }
56     }
57 
58     private function createCode()
59     {
60         $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
61 
62         for ($i = 0; $i < $this->codeNum; $i++) {
63             $this->code .= $str{rand(0, strlen($str) - 1)};
64         }
65     }
66 
67     private function setCaptcha()
68     {
69         $this->createCode();
70 
71         for ($i = 0; $i < $this->codeNum; $i++) {
72             $color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
73             $size = rand(floor($this->height / 5), floor($this->height / 3));
74             $x = floor($this->width / $this->codeNum) * $i + 5;
75             $y = rand(0, $this->height - 20);
76             imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
77         }
78     }
79 
80     private function outputImg()
81     {
82         if (imagetypes() & IMG_JPG) {
83             header('Content-type:image/jpeg');
84             imagejpeg($this->im);
85         } elseif (imagetypes() & IMG_GIF) {
86             header('Content-type: image/gif');
87             imagegif($this->im);
88         } elseif (imagetype() & IMG_PNG) {
89             header('Content-type: image/png');
90             imagepng($this->im);
91         } else {
92             die("Don't support image type!");
93         }
94     }
95 
96 }
?>

使用的一个例子:

<?php
require_once 'captcha.class.php';

$captcha = new Captcha(80,30,4);

$captcha->showImg();

?>
原文地址:https://www.cnblogs.com/sosoft/p/3466683.html