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

作者:hebedich 时间:2024-05-11 09:26:01 

效果:

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,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; $i<$this->codeNum;$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; $i<$this->pixNum; $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; $i<$this->lineNum; $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输出图像


<?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,图形验证码
0
投稿

猜你喜欢

  • Python分支语句与循环语句应用实例分析

    2022-12-02 03:35:31
  • MySQL窗口函数实现榜单排名

    2024-01-16 20:22:22
  • 模拟兼容性的 inline-block 属性

    2008-04-08 12:37:00
  • yui3的AOP(面向切面编程)和OOP(面向对象编程)

    2009-09-24 14:47:00
  • 关于超级链接的一些问题

    2007-12-07 14:00:00
  • vue项目使用md5加密、crypto-js加密、国密sm3及国密sm4的方法

    2024-04-27 15:47:47
  • 使用绿色版SQLServer2008R2出现的问题解析

    2024-01-22 23:42:58
  • 浅析Python 多行匹配模式

    2022-07-23 13:32:18
  • Django集成MongoDB实现过程解析

    2022-07-10 03:32:35
  • 《细节决定交互设计的成败》

    2009-06-02 11:23:00
  • Sql Server查询性能优化之不可小觑的书签查找介绍

    2012-05-22 18:24:53
  • Select下拉列表控件美化

    2008-11-12 12:55:00
  • Python处理键映射值操作详解

    2021-03-21 03:14:53
  • c#如何利用定时器自动备份数据库详解

    2024-01-27 12:11:33
  • python中WSGI是什么,Python应用WSGI详解

    2021-04-08 06:07:45
  • Asp中Server.ScriptTimeOut脚本超时属性需要注意的一点

    2008-10-18 14:53:00
  • go如何删除字符串中的部分字符

    2024-05-22 10:15:54
  • asp随机产生注册用户密码

    2007-10-17 12:33:00
  • Python实现变声器功能(萝莉音御姐音)

    2023-04-12 23:04:16
  • django和flask哪个值得研究学习

    2021-10-02 16:52:46
  • asp之家 网络编程 m.aspxhome.com