首页 > 作文

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

更新时间:2023-04-06 21:03:54 阅读: 评论:0

效果:

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 = '345678有奖竞猜9abcdefghijkmnpqrstuvwxyabcdefghijkmnpqrstuvwxy';
$this->checkcode = "";
for($i=0; $i&l我的老师魏巍阅读答案t;$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 tdisturbcolor()
{
/*绘制干扰点*/
for($i=0; $i<$this->pixnum; $i++)
{
$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
imagetpixel($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);
}
}
/*开启ssion保存 利用echo 输出图像*/
function __tostring()
{
$_ssion['code'] = strtoupper($this->checkcode);
$this->getcreateimage();
$this->creat高考英语试卷echeckcode();
$this->tdisturbcolor();
$this->outputimg();
}
/*内部私有方法输出图像*/
private function outputimg()
{
header("content-type:image/png");
imagepng($this->image);
}
/*析构方法,释放对象*/
function __destruct()
{
imagedestroy($this->image);
}
}
?>

imgcode.php输出图像

<?phpssion_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

本文发布于:2023-04-06 21:03:52,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/9e824e9f495e80d33b6c7fdca5dc99f8.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP制作图形验证码代码分享.doc

本文 PDF 下载地址:PHP制作图形验证码代码分享.pdf

标签:验证码   干扰   方法   个数
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图