PHP制作彩色验证码

验证码的类

制作了一个名叫Verification.class.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
<?php
class Verification{
private $text = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789';
private $code; //验证码
private $codelen = 4; //验证码长度
private $width = 120;//图像宽度
private $height = 40; //图像高度
private $font; //字体
private $fontSize = 20; //字体大小
private $img; //图像
private $line = 6; //干扰线条数量
private $snowflake = 100; //干扰雪花数量
//构造函数
public function __construct(){
$this->font = dirname(__FILE__)."/font/Elephant.ttf"; //拿到引入字体路径
}
//生成背景图
public function createBg(){
$this->img = imagecreatetruecolor($this->width, $this->height); //生成画布
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));//生成颜色
imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color); //把颜色以矩形填充画布
}
//生成验证码
private function createCode(){
$len = strlen($this->text)-1;
for($i=0;$i<$this->codelen;$i++){
$this->code .= $this->text[mt_rand(0,$len)];
}
}
//生成文字
private function createFont(){
for($i=0;$i<$this->codelen;$i++){
$color = imagecolorallocate($this->img, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155));
//用 TrueType 字体向图像写入文本
imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $this->width/$this->codelen*$i+mt_rand(1,5), $this->height/1.6, $color, $this->font, $this->code[$i]);
}
}
//生成干扰线条 和 雪花
private function createLine(){
for($i=0;$i<$this->line;$i++){ //线条
$color = imagecolorallocate($this->img, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155));//生成颜色
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
}
for($i=0;$i<$this->snowflake;$i++){ //雪花
$color = imagecolorallocate($this->img, mt_rand(180,255), mt_rand(180,255), mt_rand(180,255));//生成颜色
imagestring($this->img, mt_rand(1,5), mt_rand(0, $this->width), mt_rand(0, $this->height), "*", $color);
}
}
//输出
private function outPut(){
//Content-type:image/png
header("Content-type:image/png");
imagepng($this->img); //输出PNG格式图像
imagedestroy($this->img); //销毁图像 释放内存
}
//对外暴露接口
public function verificationCode(){
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//对外暴露code 获取验证码
public function getCode(){
return strtolower($this->code);
}
}
?>

然后通过index.php这个文件引入这个类,代码如下

1
2
3
4
5
6
7
<?php
require 'Verification.class.php';
session_start(); //开启session
$m = new Verification();
$m->verificationCode();
$_SESSION['code'] = $m->getCode(); //获取验证码保存在session里面去
?>

HTML页面

做测试的时候做了个yan.html来测试,代码如下

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
<?php
session_start(); //开启session
session_destroy(); //每次进来都销毁session
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
*{
margin:0;
padding:0;
}
input{
width:80px;
height:20px;
}
img:hover{
cursor:pointer;
}
</style>
</head>
<body>
<form action="" method="post">
<h1>验证码实例</h1>
<p>
<span>请输入验证码:</span>
<input type="text" name="code" />
<img src="index.php" onclick="this.src='index.php'" />
</p>
<p>
<input type="submit" />
</p>
<?php
if(isset($_POST['code'])){
$code = $_POST["code"];
if($code == $_SESSION['code']){
echo "<p style='color:green'>正确</p>";
}else{
echo "<p style='color:red'>错误</p>";
}
}
?>
</form>
</body>
</html>

效果图