PHP製作圖形驗證碼代碼分享_php執行個體

來源:互聯網
上載者:User

效果:

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輸出映像

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.