RSS
热门关键字:  ASP.NET  ASP  ASP.NET文件  html  ASP.NET 漫
当前位置 :| hh85>开发代码>php>

PHP图片验证码代码

来源: 作者: 时间:2008-02-17 点击:

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 < $this->numCirculos; $i++)
{
$imPoints = imagesetpixel($im, mt_rand(0,80), mt_rand(0,80), $Border);
}

//put the Code on image
for($i = 0; $i < $this->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());
}

}
?>

TAG :PHP  
上一篇:没有了
下一篇:PHP实现将汉字转换为拼音代码
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
栏目列表