您的位置:首页 > 编程语言 > PHP开发

php 制作验证码

2015-11-19 20:51 591 查看
有人多初学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

?
调用检查文件Check.php代码:

PHP code

?
总结:php做验证码总体不难,灵活运用系统提供的几个函数,以及对颜色的正确的搭配,相信你会做出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  验证码 php 扩展