国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP制作圖形驗證碼代碼分享

2020-03-22 17:56:55
字體:
供稿:網(wǎng)友
php
/*
* file:myvcode.html' target='_blank'>class.php
* 驗證碼類,類名Vcode
*/
class Vcode
{
private $width; /*驗證碼寬度*/
private $height; /*驗證碼高度*/
private $codeNum; /*驗證碼字符個數(shù)*/
private $checkCode; /*驗證碼字符*/
private $image; /*驗證碼資源*/
private $pixNum; /*繪制干擾點的個數(shù)*/
private $lineNum; /*繪制干擾線的條數(shù)*/ /*
*構(gòu)造方法實例化驗證碼對象,并初始化數(shù)據(jù)
*@param int $width 設(shè)置默認(rèn)寬度
*@param int $height 設(shè)置默認(rèn)高度
*@param int $codeNum 設(shè)置驗證碼中的字符個數(shù)
*@param int $pixNum 設(shè)置干擾點的個數(shù)
*@param int $lineNum 設(shè)置干擾線的數(shù)量
*/
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;
}
/*內(nèi)部私有方法,創(chuàng)建圖像資源*/
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);
}
/*內(nèi)部私有方法,繪制字符,去掉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);
}
}
/*內(nèi)部私有方法設(shè)置干擾元素*/
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();
}
/*內(nèi)部私有方法輸出圖像*/
private function outputImg()
{
header("content-type:image/png");
imagepng($this- image);
}
/*析構(gòu)方法,釋放對象*/
function __destruct()
{
imagedestroy($this- image);
}
}
imgcode.php輸出圖像 session_start(); require_once('myvcode.class.php'); echo new Vcode(); test.html:同過img標(biāo)簽引用
img src="imgcode.php" 可以加一個a標(biāo)簽,用js實現(xiàn)換一張效果: /*局部刷新?lián)Q驗證碼*/
function changeCode()
{
var imgcode = document.getElementById(‘code');
var change = document.getElementById(‘change');
change.onclick = function()
{
/*必須加后面的參數(shù)才能刷新*/
imgcode.src='code.php tm'+Math.random();
}
} code和change分別是img和a的idPHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜川县| 禹州市| 柯坪县| 黔江区| 涡阳县| 兰溪市| 灵璧县| 茶陵县| 邢台县| 呼图壁县| 东兰县| 隆德县| 昌乐县| 华坪县| 汉阴县| 中方县| 南安市| 海盐县| 元阳县| 团风县| 汶上县| 星座| 延津县| 高碑店市| 筠连县| 屏边| 兴隆县| 阜宁县| 祁东县| 合江县| 永仁县| 潢川县| 明溪县| 桐柏县| 浑源县| 金沙县| 镇沅| 当雄县| 黎城县| 英吉沙县| 宜兰市|