【www.gdgbn.com--代码生成】



{
$authcode = new AuthCode();
if ($authcode->GetUriFileName() == "authcode.php")
{
  $authcode->OutputImg();
}
}
function ChkAuthcode($Authcode)
{
if ($_SESSION["AuthCode"] == $Authcode)
{
  $rtn = true;
}
else
{
  $rtn = false;
}
$_SESSION["AuthCode"] = rand(0, 999999);
return $rtn;
}
class AuthCode
{
/* Public Variables */
/* Private Variables */
var $image;
#
var $sBgcolor;
var $nWidth;
var $nHeight;
var $nLen;
var $bNoise;
var $nNoise;
var $bBorder;
var $aFontlist;
/* Constractor */
function AuthCode()
{
  $this->sBgcolor = "#FFCC00";
  $this->nWidth = 70;
  $this->nHeight = 25;
  $this->nLeftMargin = 5;
  $this->nRightMargin = 5;
  $this->nTopMargin = 3;
  $this->nBottomMargin = 2;
  $this->nLen = 4;
  $this->bNoise = true;
  $this->nNoisePoint = 50;
  $this->nNoiseLine = 5;
  $this->bBorder = true;
 
  $this->aFontlist[0] = "./fonts/arial.ttf";
  $this->aFontlist[1] = "./fonts/comic.ttf";
  $this->aFontlist[2] = "./fonts/raavi.ttf";
  $this->aFontlist[3] = "./fonts/verdanai.ttf";
  $this->aFontlist[4] = "./fonts/tahoma.ttf";
  $this->aFontlist[5] = "./fonts/shruti.ttf";
  $this->aFontlist[6] = "./fonts/BKANT.TTF";
  $this->aFontlist[7] = "./fonts/comicbd.ttf";
  $this->aFontlist[8] = "./fonts/courbi.ttf";
  $this->aFontlist[9] = "./fonts/times.ttf";
}

function OutputImg()
{
  $this->image = "";
  $this->image = imagecreate($this->nWidth, $this->nHeight);
  $back = $this->getcolor($this->sBgcolor);
  imagefilledrectangle($this->image, 0, 0, $this->nWidth, $this->nHeight, $back);
  $size = ($this->nWidth - $this->nLeftMargin - $this->nRightMargin)/$this->nLen;
  if($size>($this->nHeight - $this->nTopMargin - $this->nBottomMargin))
   $size=$this->nHeight - $this->nTopMargin - $this->nBottomMargin;
 
  $left = ($this->nWidth-$this->nLen*($size+$size/10))/2 + $this->nLeftMargin;
  $code = "";
  for ($i=0; $i<$this->nLen; $i++)
  {
   $randtext = rand(0, 9);
   $code .= $randtext;
   $textColor = imagecolorallocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
   $font = $this->aFontlist[rand(0,9)];//rand(1,4).".ttf";
   $randsize = rand($size-$size/10, $size+$size/10);
   $location = $left+($i*$size+$size/10);
   imagettftext($this->image, $randsize, rand(-18,18), $location, rand($size, $size+$size/5) + $this->nTopMargin, $textColor, $font, $randtext);
  }
  if($this->bNoise == true) $this->setnoise();
  $_SESSION["AuthCode"] = $code;
  $bordercolor = $this->getcolor("      ");
  if($border==true) imagerectangle($this->image, 0, 0, $this->nWidth-1, $this->nHeight-1, $bordercolor);
  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // date in the past
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Cache-Control: private");
  header("Pragma: no-cache"); // HTTP/1.0
  header("Content-type: image/png");
  imagepng($this->image);
  imagedestroy($this->image);
 
  return $sAuthcode;
}
function ChkAuthcode($Authcode)
{
  if ($this->GetAuthcode() == $Authcode)
  {
   $rtn = true;
  }
  else
  {
   $rtn = false;
  }
  $_SESSION["AuthCode"] = rand(0, 999999);
  return $rtn;
}
function GetAuthcode()
{
  $x_AuthCode = $_SESSION["AuthCode"];
  $_SESSION["AuthCode"] = rand(0, 999999);
  return $x_AuthCode;
}
 
/* Private Functions */
function GetUriFileName()
{
  return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1 , strlen($_SERVER["SCRIPT_NAME"]) - strrpos($_SERVER["SCRIPT_NAME"], "/"));
}
function setnoise()
{
  for ($i=0; $i<$this->nNoiseLine; $i++){
   $randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
   imageline($this->image, rand(0, $this->nWidth), rand(0, $this->nHeight), rand(0, $this->nWidth), rand(0, $this->nHeight), $randColor);
  }
 
  for ($i=0; $i<$this->nNoisePoint; $i++){
   $randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)); 
   imagesetpixel($this->image, rand(0, $this->nWidth), rand(0, $this->nHeight), $randColor);
  }
}
function getcolor($color)
{
   $color = eregi_replace ("^#","",$color);
   $r = $color[0].$color[1];
   $r = hexdec ($r);
   $b = $color[2].$color[3];
   $b = hexdec ($b);
   $g = $color[4].$color[5];
   $g = hexdec ($g);
   $color = imagecolorallocate ($this->image, $r, $b, $g);
   return $color;
}
}
?>

本文来源:http://www.gdgbn.com/wangyetexiao/15772/