php图形验证码验证,PHP制作图形验证码代码分享

本文介绍了一个名为Vcode的PHP类,用于生成验证码。该类包含了创建图像资源、绘制字符、设置干扰元素以及输出图像的方法。通过实例化类并设置参数,可以创建不同尺寸和样式的验证码。同时,示例代码展示了如何在PHP页面中使用该类并将其输出到HTML页面中,以及如何利用JavaScript实现验证码的局部刷新功能。
摘要由CSDN通过智能技术生成

效果:

b455807696f7090a8298f0e4b3387c9e.png

6669747c6dab48799d5062cf3e389386.png

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

/*

* file:myvcode.class.php

* 验证码类,类名Vcode

*/

class Vcode

{

private $width;              /*验证码宽度*/

private $height;             /*验证码高度*/

private $codeNum;            /*验证码字符个数*/

private $checkCode;            /*验证码字符*/

private $image;                /*验证码资源*/

private $pixNum;            /*绘制干扰点的个数*/

private $lineNum;            /*绘制干扰线的条数*/

/*

*构造方法实例化验证码对象,并初始化数据

*@param int $width         设置默认宽度

*@param int $height     设置默认高度

*@param int $codeNum    设置验证码中的字符个数

*@param int $pixNum        设置干扰点的个数

*@param int $lineNum    设置干扰线的数量

*/

function __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)

{

$this->width = $width;

$this->height = $height;

$this->codeNum = $codeNum;

$this->pixNum = $pixNum;

$this->lineNum = $lineNum;

}

/*内部私有方法,创建图像资源*/

private function getCreateImage()

{

$this->image = imagecreatetruecolor($this->width, $this->height);

$white = imagecolorallocate($this->image,0xff,0xff,0xff);

imagefill($this->image, 0, 0, $white);

$black = imagecolorallocate($this->image,0,0,0);

imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);

}

/*内部私有方法,绘制字符,去掉o0Llz和012*/

private function createCheckCode()

{

$code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';

$this->checkCode = "";

for($i=0; $icodeNum;$i++)

{

$char = $code{rand(0,strlen($code) - 1)};

$this->checkCode .= $char;

$fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));

$fontSize = rand(3,5);

$x = rand(0,$this->width-imagefontwidth($fontSize));

$y = rand(0,$this->height-imagefontheight($fontSize));

imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);

}

}

/*内部私有方法设置干扰元素*/

private function setDisturbColor()

{

/*绘制干扰点*/

for($i=0; $ipixNum; $i++)

{

$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));

imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);

}

/*绘制干扰线*/

for($i=0; $ilineNum; $i++)

{

$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));

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);

}

}

/*开启session保存 利用echo 输出图像*/

function __toString()

{

$_SESSION['code'] = strtoupper($this->checkCode);

$this->getCreateImage();

$this->createCheckCode();

$this->setDisturbColor();

$this->outputImg();

}

/*内部私有方法输出图像*/

private function outputImg()

{

header("content-type:image/png");

imagepng($this->image);

}

/*析构方法,释放对象*/

function __destruct()

{

imagedestroy($this->image);

}

}

?>

imgcode.php输出图像

session_start();

require_once('myvcode.class.php');

echo new Vcode();

?>

test.html:同过img标签引用

imgcode.php

可以加一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值