这个利用PHP动态随机生成验证码的类是在LAMP的书上看到的。感觉写的很好就收藏了下来。下面是效果图,这个效果图是没有开启干扰码的效果图

下面是类代码

 

 

PHP生成动态验证码类代码
1 <?php
2 /************************************************************************
3 //FILE:ImageCode
4 //DONE:生成动态验证码类
5 //DATE"2010-3-31
6 //Author:www.5dkx.com 5D开心博客
7 ************************************************************************/
8 class ImageCode{
9 private $width; //验证码图片宽度
10   private $height; //验证码图片高度
11   private $codeNum; //验证码字符个数
12   private $checkCode; //验证码字符
13   private $image; //验证码画布
14 /************************************************************************
15 // Function:构造函数
16 // Done:成员属性初始化
17 // Author:www.5dkx.com 5D开心博客
18 ************************************************************************/
19 function __construct($width=60,$height=20,$codeNum=4)
20 {
21 $this->width = $width;
22 $this->height = $height;
23 $this->codeNum = $codeNum;
24 $this->checkCode = $this->createCheckCode();
25 }
26 function showImage()
27 {
28 $this->getcreateImage();
29 $this->outputText();
30 $this->setDisturbColor();
31 $this->outputImage();
32 }
33 function getCheckCode()
34 {
35 return $this->chekCode;
36 }
37 private function getCreateImage()
38 {
39 $this->image = imagecreatetruecolor($this->width,$this->height);
40 $back = imagecolorallocate($this->image,255,255,255);
41 $border = imagecolorallocate($this->image,255,255,255);
42 imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
43 //使用纯白色填充矩形框,这里用的话后面干扰码失效
44 /*如果想用干扰码的话使用下面的*/
45 //imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
46 }
47 private function createCheckCode()
48 {
49 for($i=0;$i<$this->codeNum;$i++)
50 {
51 $number = rand(0,2);
52 switch($number)
53 {
54 case 0: $rand_number = rand(48,57); break;//数字
55 case 1: $rand_number = rand(65,90);break;//大写字母
56 case 2: $rand_number = rand(97,122);break;//小写字母
57 }
58 $asc = sprintf("%c",$rand_number);
59 $asc_number = $asc_number.$asc;
60 }
61 return $asc_number;
62 }
63 private function setDisturbColor()//干扰吗设置
64 {
65 for($i=0;$i<=100;$i++)
66 {
67 //$color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
68 $color = imagecolorallocate($this->image,255,255,255);
69 imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
70 }
71 //$color = imagecolorallocate($this->image,0,0,0);
72 //imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
73
74
75 }
76 private function outputText()
77 {
78 //随机颜色、随机摆放、随机字符串向图像输出
79 for($i=0;$i<=$this->codeNum;$i++)
80 {
81 $bg_color = imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255));
82 $x = floor($this->width/$this->codeNum)*$i+3;
83 $y = rand(0,$this->height-15);
84 imagechar($this->image,5,$x,$y,$this->checkCode[$i],$bg_color);
85 }
86 }
87
88 private function outputImage()
89 {
90 if(imagetypes()&IMG_GIF)
91 {
92 header("Content_type:image/gif");
93 imagegif($this->image);
94 }
95 elseif(imagetypes()&IMG_JPG)
96 {
97
98 header("Content-type:image/jpeg");
99 imagejpeg($this->image,"",0.5);
100 }
101 elseif(imagetypes()&IMG_PNG)
102 {
103 header("Content-type:image/png");
104 imagejpeg($this->image);
105 }
106 elseif(imagetypes()&IMG_WBMP)
107 {
108 header("Content-type:image/vnd.wap.wbmp");
109 imagejpeg($this->image);
110 }
111 else
112 {
113 die("PHP不支持图像创建");
114 }
115 }
116
117 function __destruct()
118 {
119 imagedestroy($this->image);
120 }
121 }
122
123 /*显示*/
124 /*******************************************************************
125 session_start();
126 $image = new ImageCode(60,20,4);
127 $image->showImage();
128 $_SESSION['ImageCode'] = $image->getCheckCode();
129 *******************************************************************/
130
131 ?>
132
133
134

非特别说明均为原创文章如转载,请注明:转载自 5D开心博客 [ http://www.5DKX.com/ ]

首发:http://www.5dkx.com/arch/157.html

posted on 2010-04-07 14:56  5D开心博客  阅读(1322)  评论(2编辑  收藏  举报