php 制作验证码

有人多初学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文件代码:
PHP code
?
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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值