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


Posted in PHP onOctober 23, 2014

效果:

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 相关文章推荐
用PHP和ACCESS写聊天室(二)
Oct 09 PHP
在php MYSQL中插入当前时间
Apr 06 PHP
php初学者写及时补给skype用户充话费的小程序
Nov 02 PHP
php面向对象全攻略 (九)访问类型
Sep 30 PHP
php radio 单选框获取与保持值的实现代码
May 15 PHP
PHP 显示客户端IP与服务器IP的代码
Oct 12 PHP
PHP超级全局变量数组小结
Oct 04 PHP
php查看请求头信息获取远程图片大小的方法分享
Dec 25 PHP
zf框架的校验器InArray使用示例
Mar 13 PHP
ThinkPHP使用心得分享-分页类Page的用法
May 15 PHP
网站防止被刷票的一些思路与方法
Jan 08 PHP
PHP实现动态获取函数参数的方法示例
Apr 02 PHP
PHP链接MySQL的常用扩展函数
Oct 23 #PHP
使用PHPMailer实现邮件发送代码分享
Oct 23 #PHP
PHP封装分页函数实现文本分页和数字分页
Oct 23 #PHP
20个2014年最优秀的PHP框架回顾
Oct 22 #PHP
PHP获取当前页面URL函数实例
Oct 22 #PHP
PHP连接MSSQL2008/2005数据库(SQLSRV)配置实例
Oct 22 #PHP
百度实时推送api接口应用示例
Oct 21 #PHP
You might like
利用PHP和AJAX创建RSS聚合器的代码
2007/03/13 PHP
php数组应用之比较两个时间的相减排序
2008/08/18 PHP
如何使用“PHP” 彩蛋进行敏感信息获取
2013/08/07 PHP
php实现cookie加密的方法
2015/03/10 PHP
PHP基于session.upload_progress 实现文件上传进度显示功能详解
2019/08/09 PHP
Centos7.7 64位利用本地完整安装包安装lnmp/lamp套件教程
2021/03/09 Servers
不要使用jQuery触发原生事件的方法
2014/03/03 Javascript
Vue.js每天必学之内部响应式原理探究
2016/09/07 Javascript
Servlet实现文件上传,可多文件上传示例
2016/12/05 Javascript
es6的数字处理的方法(5个)
2017/03/16 Javascript
JS计算输出100元钱买100只鸡问题的解决方法
2018/01/04 Javascript
解决layui批量传值到后台操作时出现传值为空的问题
2019/09/28 Javascript
js实现金山打字通小游戏
2020/07/24 Javascript
[03:42]2018完美盛典-《加冕》
2018/12/16 DOTA
[55:54]FNATIC vs EG 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
python基于windows平台锁定键盘输入的方法
2015/03/05 Python
在Python程序中操作文件之flush()方法的使用教程
2015/05/24 Python
Python文件与文件夹常见基本操作总结
2016/09/19 Python
python 容器总结整理
2017/04/04 Python
Python实现字符串与数组相互转换功能示例
2017/09/22 Python
对python中Matplotlib的坐标轴的坐标区间的设定实例讲解
2018/05/25 Python
对python列表里的字典元素去重方法详解
2019/01/21 Python
python图形开发GUI库wxpython使用方法详解
2020/02/14 Python
Python列表切片常用操作实例解析
2020/03/10 Python
Nike香港官网:Nike HK
2019/03/23 全球购物
广州御银科技股份有限公司试卷(C++)
2016/11/04 面试题
UNIX特点都有哪些
2016/04/05 面试题
公司董事长职责
2013/12/12 职场文书
2014学习全国两会精神心得体会2000字
2014/03/11 职场文书
节能宣传周活动总结
2014/05/08 职场文书
法律专业大学生职业生涯规划书:向目标一步步迈进
2014/09/22 职场文书
2014最新实习证明模板
2014/10/02 职场文书
2016中秋节广告语
2016/01/28 职场文书
准备去美国留学,那么大学申请文书应该怎么写?
2019/08/12 职场文书
Pytorch 如何实现常用正则化
2021/05/27 Python
Python获取江苏疫情实时数据及爬虫分析
2021/08/02 Python