Effect:
Myvcode.class.php: Encapsulates the class that creates the validation code
<?php
/*
* file:myvcode.class.php
* Verification code class, class name Vcode
*/
Class Vcode
{
Private $width; /* Verify Code Width * *
Private $height; /* Verification Code Height * *
Private $codeNum; /* Verify Code character number * *
Private $checkCode; /* Verification Code character/*
Private $image; /* Authentication Code resource * *
Private $pixNum; /* Draw the number of interference points * *
Private $lineNum; * * Draw the number of interference lines/
/*
* Constructs a method to instantiate a validation code object and initialize the data
* @param int $width Set the default width
* @param int $height Set the default height
* @param int $codeNum Set the number of characters in the verification code
* @param int $pixNum Set the number of jamming points
* @param int $lineNum Set the number of interference lines
*/
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;
}
/* Internal private method, create image resource * *
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);
}
/* Internal private method, draw characters, remove O0llz and 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);
}
}
/* Internal private method to set the interference element * *
Private Function Setdisturbcolor ()
{
/* Draw Interference Point * *
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);
}
/* Draw Interference Line * *
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);
}
}
/* Open session Save using ECHO output image * *
function __tostring ()
{
$_session[' Code ' = Strtoupper ($this->checkcode);
$this->getcreateimage ();
$this->createcheckcode ();
$this->setdisturbcolor ();
$this->outputimg ();
}
/* Internal Private method Output image * *
Private Function Outputimg ()
{
Header ("Content-type:image/png");
Imagepng ($this->image);
}
/* destructor, Release object/*
function __destruct ()
{
Imagedestroy ($this->image);
}
}
?>
imgcode.php Output Image
<?php
session_start ();
Require_once (' myvcode.class.php ');
echo New Vcode ();
? >
test.html: The same IMG tag reference
Can add a a label, using JS to achieve a change in effect:
/* Local Refresh Change Verification code * *
function ChangeCode ()
{
var imgcode = document.getElementById (' code ');
var change = document.getElementById (' change ');
Change.onclick = function ()
{
/* must add the following parameters to refresh */
Imgcode.src= ' Code.php?tm ' +math.random ();
}
}
Code and change are IDs of IMG and a respectively