Home > php教程 > php手册 > PHP图片验证码类

PHP图片验证码类

WBOY
Release: 2016-06-21 09:05:19
Original
1064 people have browsed it

验证码


class validateImage
{

var $x;
var $y;
var $numChars;
var $Code;
var $Width;
var $Height;
var $BG;
var $colTxt;
var $colBorder;
var $numCirculos;

//构造函数、初始值
function validateImage()
{
$this->x = $x;
$this->y = $y = "6";
$this->numChars = $numChars = "4"; //Number of Code
$this->Code = $Code;
$this->Width = $Width = "80"; //Width of Image
$this->Height = $Height = "25"; //Height of Image
$this->BG = $BG = "255 255 255"; //RGB color of background
$this->colTxt = $colTxt = "0 0 0 0"; //RGB color of code
$this->Border = $colBorder = "100 100 100"; //RGB color of Border
$this->numCirculos = $numCirculos = "800"; //Number of random point
}

//Create base Image
function createImage()
{
//Create a image
$im = imagecreate ($this->Width, $this->Height) or die ("Cannot Initialize new GD image stream");

//Get the RGB color code
$colorBG = explode(" ", $this->BG);

$colorBorder = explode(" ", $this->Border);

$colorTxt = explode(" ", $this->colTxt);

//put the background color on the image
$imBG = imagecolorallocate ($im, $colorBG[0], $colorBG[1], $colorBG[2]);

//put the border on the image
$Border = ImageColorAllocate($im, $colorBorder[0], $colorBorder[1], $colorBorder[2]);
$imBorder = ImageRectangle($im, 0, 0, $this->Width-1,$this->Height-1, $Border);

//put the code color on the image
$imTxt = imagecolorallocate ($im, $colorTxt[0], $colorTxt[1], $colorTxt[2]);

//Drop 800 points
for($i = 0; $i numCirculos; $i++)
{
$imPoints = imagesetpixel($im, mt_rand(0,80), mt_rand(0,80), $Border);
}

//put the Code on image
for($i = 0; $i numChars; $i++)
{
//get $x's location
$this->x = 21 * $i + 5;

//get the code
mt_srand((double) microtime() * 1000000*getmypid());
$this->Code.= (mt_rand(0, 9));

$putCode = substr($this->Code, $i, "1");

//put the code;
$Code = imagestring($im, 5, $this->x, $this->y, $putCode,$imTxt);

}

return $im;

}


//Transfer the code to next page
function transferCode()
{
//get the code
$this->createImage();
$vCode = $this->Code;

session_start();
session_register(vCode);

$_SESSION['validate_code'] = $vCode;

return $vCode;
}


//display the image
function show()
{
header("Content-type:image/png");
Imagepng($this->createImage());
Imagedestroy($this->createImage());
}

}
?>



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template