有人多初学php者,不知道如何使用php制作验证码,今天笔者在这里就这个问题进行归纳总结一下(英文字母和数字组成的验证码)。能力有限,有瑕疵之处,欢迎批评指正。
环境准备:php开启了GD扩展库
开始制作画布
函数准备:
制作画布的函数:
imagecreatetruecolor — 新建一个真彩色图像
说明
resource imagecreatetruecolor ( int $width , int $height )
基于调色板的图像分配背景色函数:
imagecolorallocate — 为一幅图像分配颜色
说明
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
为画布填充颜色
imagefill — 区域填充
说明
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
我们做一个简单的验证码类Chptcha.class.php
在同级目录下创建一文件用来对制作的验证码调用个check.php
<?php
class Chptcha{
private $width=200;
private $height=100;
private $chars=5;
private $lines=20;
//干扰点
private $spots=50;
public function generate(){
//制作画布
$img=imagecreatetruecolor($this->width,$this->height);
//在画布资源下分配颜色,经验,画布颜色要明亮点
$bg=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
//给画布填充颜色
imagefill($img,0,0,$bg);
//在浏览器上显示创建的图片
header('content-type:image/png');
imagepng($img);
}
}
在check.php文件中调用方法得到画布
为画布增加验证码
函数准备:
增加内容的函数
imagestring — 水平地画一行字符串
说明
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
得到验证码的随机数;开发经验:去掉容易干扰的零和O,L和1等
在Captcha.class.php中增加方法
private function getCaptcha(){
//产生随机字符串
$str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
$captcha = '';
for($i = 0;$i < $this->chars;$i++){
$captcha .= $str[mt_rand(0,strlen($str) - 1)];
}
//返回
return $captcha;
}
在Captcha.class.php中的generate()方法下
//增加验证码
$captcha=$this->getCaptcha();
$str=imagecolorallocate($img,mt_rand(50,100),mt_rand(50,100),mt_rand(50,100));
//获取随机位置
//宽度: 使用图片宽度减去文件宽度
$e_width = $this->width - $this->chars * 10 - 10;
$e_height = $this->height/2;
//5,代表的是字体的大小 imagestring($img,5,mt_rand(10,$e_width),mt_rand($e_height-1,$e_height),$captcha,$str);
header('content-type:image/png');
imagepng($img);
}
效果:
增加干扰线
函数准备:
制作干扰线的函数
imageline — 画一条线段
说明
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段
增加干扰线的方法:
private function getLines($img){
//增加干扰线
for($i = 0;$i < $this->lines;$i++){
//分配颜色
$line=imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
//制作线段 imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line);
}
}
效果:
增加干扰点
函数准备:
imagesetpixel — 画一个单一像素
说明
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
增加干扰点的方法:
private function getPixels($img){
//增加干扰点
for($i = 0;$i < $this->spots;$i++){
//分配颜色
$pixel= imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
//制作点
imagesetpixel($img,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel);
}
}
效果
Chptcha.class.php文件代码:
调用检查文件Check.php代码:
环境准备:php开启了GD扩展库
开始制作画布
函数准备:
制作画布的函数:
imagecreatetruecolor — 新建一个真彩色图像
说明
resource imagecreatetruecolor ( int $width , int $height )
基于调色板的图像分配背景色函数:
imagecolorallocate — 为一幅图像分配颜色
说明
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
为画布填充颜色
imagefill — 区域填充
说明
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
我们做一个简单的验证码类Chptcha.class.php
在同级目录下创建一文件用来对制作的验证码调用个check.php
<?php
class Chptcha{
private $width=200;
private $height=100;
private $chars=5;
private $lines=20;
//干扰点
private $spots=50;
public function generate(){
//制作画布
$img=imagecreatetruecolor($this->width,$this->height);
//在画布资源下分配颜色,经验,画布颜色要明亮点
$bg=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
//给画布填充颜色
imagefill($img,0,0,$bg);
//在浏览器上显示创建的图片
header('content-type:image/png');
imagepng($img);
}
}
在check.php文件中调用方法得到画布

为画布增加验证码
函数准备:
增加内容的函数
imagestring — 水平地画一行字符串
说明
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
得到验证码的随机数;开发经验:去掉容易干扰的零和O,L和1等
在Captcha.class.php中增加方法
private function getCaptcha(){
//产生随机字符串
$str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
$captcha = '';
for($i = 0;$i < $this->chars;$i++){
$captcha .= $str[mt_rand(0,strlen($str) - 1)];
}
//返回
return $captcha;
}
在Captcha.class.php中的generate()方法下
//增加验证码
$captcha=$this->getCaptcha();
$str=imagecolorallocate($img,mt_rand(50,100),mt_rand(50,100),mt_rand(50,100));
//获取随机位置
//宽度: 使用图片宽度减去文件宽度
$e_width = $this->width - $this->chars * 10 - 10;
$e_height = $this->height/2;
//5,代表的是字体的大小 imagestring($img,5,mt_rand(10,$e_width),mt_rand($e_height-1,$e_height),$captcha,$str);
header('content-type:image/png');
imagepng($img);
}
效果:

增加干扰线
函数准备:
制作干扰线的函数
imageline — 画一条线段
说明
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段
增加干扰线的方法:
private function getLines($img){
//增加干扰线
for($i = 0;$i < $this->lines;$i++){
//分配颜色
$line=imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
//制作线段 imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line);
}
}
效果:

增加干扰点
函数准备:
imagesetpixel — 画一个单一像素
说明
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
增加干扰点的方法:
private function getPixels($img){
//增加干扰点
for($i = 0;$i < $this->spots;$i++){
//分配颜色
$pixel= imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
//制作点
imagesetpixel($img,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel);
}
}
效果

Chptcha.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
<?php
class
Chptcha{
private
$width
=200;
private
$height
=100;
private
$chars
=5;
private
$lines
=20;
//干扰点
private
$spots
=600;
public
function
generate(){
//制作画布
$img
=imagecreatetruecolor(
$this
->width,
$this
->height);
//在画布资源下分配颜色,经验,画布颜色要明亮点
$bg
=imagecolorallocate(
$img
,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
//给画布填充颜色
imagefill(
$img
,0,0,
$bg
);
//增加验证码
$captcha
=
$this
->getCaptcha();
$str
=imagecolorallocate(
$img
,mt_rand(50,100),mt_rand(50,100),mt_rand(50,100));
//获取随机位置
//宽度: 使用图片宽度减去文件宽度
$e_width
=
$this
->width -
$this
->chars * 10 - 10;
$e_height
=
$this
->height/2;
//5,代表的是字体的大小
imagestring(
$img
,5,mt_rand(10,
$e_width
),mt_rand(
$e_height
-1,
$e_height
),
$captcha
,
$str
);
$this
->getLines(
$img
);
$this
->getPixels(
$img
);
header(
'content-type:image/png'
);
imagepng(
$img
);
//释放资源
imagedestroy(
$img
);
}
private
function
getCaptcha(){
//产生随机字符串
$str
=
'abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789'
;
$captcha
=
''
;
for
(
$i
= 0;
$i
<
$this
->chars;
$i
++){
$captcha
.=
$str
[mt_rand(0,
strlen
(
$str
) - 1)];
}
//返回
return
$captcha
;
}
private
function
getLines(
$img
){
//增加干扰线
for
(
$i
= 0;
$i
<
$this
->lines;
$i
++){
//分配颜色
$line
= imagecolorallocate(
$img
,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
//制作线段
imageline(
$img
,mt_rand(0,
$this
->width),mt_rand(0,
$this
->height),mt_rand(0,
$this
->width),mt_rand(0,
$this
->height),
$line
);
}
}
private
function
getPixels(
$img
){
//增加干扰点
for
(
$i
= 0;
$i
<
$this
->spots;
$i
++){
//分配颜色
$pixel
= imagecolorallocate(
$img
,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
//制作
imagesetpixel(
$img
,mt_rand(0,
$this
->width),mt_rand(0,
$this
->height),
$pixel
);
}
}
}
|
调用检查文件Check.php代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php
//自动加载类函数
header(
'content-type:text/html;charset=utf-8'
);
function
__autoload(
$class
){
$file
=
"$class.class.php"
;
//判断是否是一个文件
if
(
is_file
(
$file
)){
include_once
$file
;
}
}
//创建对象
$chptcha
=
new
Chptcha();
//调用方法
$chptcha
->generate();
|
总结:php做验证码总体不难,灵活运用系统提供的几个函数,以及对颜色的正确的搭配,相信你会做出。