博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP制作验证码
阅读量:4286 次
发布时间:2019-05-27

本文共 3925 字,大约阅读时间需要 13 分钟。

效果:

myvcode.class.php:封装创建验证码的类

1: 
2: /*
3: * file:myvcode.class.php
4: * 验证码类,类名Vcode
5: */
6: class Vcode
7: {
8:     private $width;              /*验证码宽度*/
9:     private $height;             /*验证码高度*/
10:     private $codeNum;            /*验证码字符个数*/
11:     private $checkCode;            /*验证码字符*/
12:     private $image;                /*验证码资源*/
13:     private $pixNum;            /*绘制干扰点的个数*/
14:     private $lineNum;            /*绘制干扰线的条数*/
15:
16:     /*
17:     *构造方法实例化验证码对象,并初始化数据
18:     *@param int $width         设置默认宽度
19:     *@param int $height     设置默认高度
20:     *@param int $codeNum    设置验证码中的字符个数
21:     *@param int $pixNum        设置干扰点的个数
22:     *@param int $lineNum    设置干扰线的数量
23:     */
24:     function __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)
25:     {
26:         $this->width = $width;
27:         $this->height = $height;
28:         $this->codeNum = $codeNum;
29:         $this->pixNum = $pixNum;
30:         $this->lineNum = $lineNum;
31:     }
32:     /*内部私有方法,创建图像资源*/
33:     private function getCreateImage()
34:     {
35:         $this->image = imagecreatetruecolor($this->width, $this->height);
36:         $white = imagecolorallocate($this->image,0xff,0xff,0xff);
37:         imagefill($this->image, 0, 0, $white);
38:         $black = imagecolorallocate($this->image,0,0,0);
39:         imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);
40:     }
41:     /*内部私有方法,绘制字符,去掉o0Llz和012*/
42:     private function createCheckCode()
43:     {
44:         $code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';
45:         $this->checkCode = "";
46:         for($i=0; $i<$this->codeNum;$i++)
47:         {
48:             $char = $code{rand(0,strlen($code) - 1)};
49:             $this->checkCode .= $char;
50:             $fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));
51:             $fontSize = rand(3,5);
52:             $x = rand(0,$this->width-imagefontwidth($fontSize));
53:             $y = rand(0,$this->height-imagefontheight($fontSize));
54:             imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);
55:         }
56:     }
57:     /*内部私有方法设置干扰元素*/
58:     private function setDisturbColor()
59:     {
60:         /*绘制干扰点*/
61:         for($i=0; $i<$this->pixNum; $i++)
62:         {
63:             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
64:             imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
65:         }
66:         /*绘制干扰线*/
67:         for($i=0; $i<$this->lineNum; $i++)
68:         {
69:             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));

70: imageline($this->image, rand(1,$this->width / 2), rand(1,$this->height / 2),

rand($this->width / 2,$this->width – 2), rand($this->height / 2,$this->height – 2), $color);

71:         }
72:     }
73:     /*开启session保存 利用echo 输出图像*/
74:     function __toString()
75:     {
76:         $_SESSION['code'] = strtoupper($this->checkCode);
77:         $this->getCreateImage();
78:         $this->createCheckCode();
79:         $this->setDisturbColor();
80:         $this->outputImg();
81:     }
82:     /*内部私有方法输出图像*/
83:     private function outputImg()
84:     {
85:         header("content-type:image/png");
86:         imagepng($this->image);
87:     }
88:     /*析构方法,释放对象*/
89:     function __destruct()
90:     {
91:         imagedestroy($this->image);
92:     }
93: }
94: ?>

imgcode.php输出图像

1: 
2: session_start();
3: require_once('myvcode.class.php');
4: echo new Vcode();
5: ?>

test.html:同过img标签引用

1: 
可以加一个a标签,用js实现换一张效果:

/*局部刷新换验证码*/function changeCode(){	var imgcode = document.getElementById('code');	var change = document.getElementById('change');	change.onclick = function()	{		/*必须加后面的参数才能刷新*/		imgcode.src='code.php?tm'+Math.random();	}}
code和change分别是img和a的id

你可能感兴趣的文章
Postman(get/post)
查看>>
飞行的小球
查看>>
小球撞墙
查看>>
下大雪
查看>>
上下左右移动小球
查看>>
打字母的游戏
查看>>
鼠标孔令志小球
查看>>
为什么谷歌浏览器无法添加扩展程序
查看>>
swichomege安装
查看>>
复制文件File
查看>>
复制大文件
查看>>
Git使用
查看>>
文件加密与解密
查看>>
jsonp修改 增加callback
查看>>
Fiddler抓包8-打断点(bpu)
查看>>
Python安装和安装selenium
查看>>
python接口自动化1-发送get请求 request
查看>>
No module named 'email.mime'; 'email' is not a package
查看>>
name 'raw_input' is not defined
查看>>
画乌龟
查看>>