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

PHP图片验证码类

WBOY
WBOYOriginal
2016-06-21 09:05:191053Durchsuche

验证码


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());
}

}
?>



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP链接ACCESS数据库简单的方法Nächster Artikel:PHP 编码规范