加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 02:05:28 所属栏目:PHP教程 来源:网络整理
导读:《:PHP制作图形验证码代码分享》要点: 本文介绍了:PHP制作图形验证码代码分享,希望对您有用。如果有疑问,可以联系我们。 效果: PHP应用 myvcode.class.php:封装创立验证码的类 ?php /* * file:myvcode.class.php * 验证码类,类名Vcode */ class Vcode

《:PHP制作图形验证码代码分享》要点:
本文介绍了:PHP制作图形验证码代码分享,希望对您有用。如果有疑问,可以联系我们。

效果:PHP应用

:PHP制作图形验证码代码分享:PHP制作图形验证码代码分享

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

<?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);
imagefill($this->image,$white);
$black = imagecolorallocate($this->image,0);
imagerectangle($this->image,$this->width-1,$this->height-1,$black);
}
/*内部私有办法,绘制字符,去掉o0Llz和012*/
private function createCheckCode()
{
$code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';
$this->checkCode = "";
for($i=0; $i<$this->codeNum;$i++)
{
$char = $code{rand(0,strlen($code) - 1)};
$this->checkCode .= $char;
$fontColor = imagecolorallocate($this->image,rand(0,128),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; $i<$this->pixNum; $i++)
{
$color = imagecolorallocate($this->image,255),255));
imagesetpixel($this->image,rand(1,$this->width-2),$this->height-2),$color);
}
/*绘制干扰线*/
for($i=0; $i<$this->lineNum; $i++)
{
$color = imagecolorallocate($this->image,255));
imageline($this->image,$this->width / 2),$this->height / 2),
rand($this->width / 2,$this->width C 2),rand($this->height / 2,$this->height C 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输出图像

<?php
session_start();
require_once('myvcode.class.php');
echo new Vcode();
?>

test.html:同过img标签引用

<img src="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

《:PHP制作图形验证码代码分享》是否对您有启发,欢迎查看更多与《:PHP制作图形验证码代码分享》相关教程,学精学透。编程之家 52php.cn为您提供精彩教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读