PHP Tutorial generates CAPTCHA code class
*/
Class SecureCode
{
private static $instance =null;
Private $code = ';
Private $fontfile;
Private $validate;
Private $image;
Private $specialadd = ' special string for SecureCode ';
Private $codeexpire = 86400;
Private $codecookiename = ' secure_code ';
/**
* Construction Method
*/
Private Function SecureCode ()
{
$this->fontfile = dirname (__file__). '/arial.ttf ';
}
Private Function __construct ()
{
$this->securecode ();
}
public static function getinstance ()
{
if (self:: $instance ==null)
Self:: $instance =new self ();
Return self:: $instance;
}
/**
* Specify the path of the font file, default to the current folder under the Arial.ttf file
* @param $fontfile file path
* @return void
*/
function Loadfont ($fontfile)
{
$this->fontfile = $fontfile;
}
/**
* Image output method, the program should not have any form of output before executing this method
* @return void;
*/
function Stroke ()
{
$this->savecode ();
Self::sendheader ();
Imagegif ($this->validate);
Imagedestroy ($this->validate);
Imagedestroy ($this->image);
}
/**
* Picture Save Method
* @param $filename save path
* @return void
*/
Function Save ($filename)
{
$this-&G T;savecode ();
Imagegif ($this->validate, $filename);
Imagedestroy ($this->validate);
Imagedestroy ($this->image);
}
/**
* Verification Code validation Method
* @param $input the string to validate, that is, the user's input
* @return Boolean validation result
*/
Function Veri FY ($input)
{
$input =strtolower ($input),
$targetcode = $this->authcode ($input);
$code = $this GetCookie ();
if (Empty ($code) | | $code! = $targetcode)
$result = false;
Else
$result =true;
$_cookie[$this->codecookiename]= ";
Setcookie ($this->codecookiename, ",-1);
return $result;
}
/**
* Image Creation Method
* @return void;
*/
function CreateImage ()
{
$this->randcode ();
$size = 30;
$width = 90;
$height = 35;
$degrees = Array (
Rand (0, +), rand (0, +), rand (0, +), rand (0, 30)
);
for ($i = 0; $i < 4; + + $i)
{
if (rand ()% 2);
else $degrees [$i] =-$degrees [$i];
}
$this->image = Imagecreatetruecolor ($size, $size);
$this->validate = Imagecreatetruecolor ($width, $height);
$back = Imagecolorallocate ($this->image, 255, 255, 255);
$border = Imagecolorallocate ($this->image, 0, 0, 0);
Imagefilledrectangle ($this->validate, 0, 0, $width, $height, $back);
for ($i = 0; $i < 4; + + $i)
{
$temp = SELF::RGBTOHSV (rand (0, +), rand (0, $), rand (0, 250));
if ($temp [2] >) $temp [2] = 60;
$temp = Self::hsvtorgb ($temp [0], $temp [1], $temp [2]);
$textcolor [$i] = imagecolorallocate ($this->image, $temp [0], $temp [1], $temp [2]);
}
for ($i = 0; $i <; + + $i)
{
$randpixelcolor = Imagecolorallocate ($this->validate, rand (0, 255), rand (0, 255), rand (0, 255));
Imagesetpixel ($this->validate, rand (1, a), rand (1, +), $randpixelcolor);
}
$temp = SELF::RGBTOHSV (rand (255), Rand (UP, 255), rand (220, 255));
if ($temp [2] <) $temp [2] = 255;
$temp = Self::hsvtorgb ($temp [0], $temp [1], $temp [2]);
$randlinecolor = Imagecolorallocate ($this->image, $temp [0], $temp [1], $temp [2]);
Self::imagelinethick ($this->validate, $textcolor [rand (0, 3)]);
Imagefilledrectangle ($this->image, 0, 0, $size, $size, $back);
Putenv (' gdfontpath= '. Realpath ('. '));
Name the font to be used (note the lack of the. ttf extension
Imagettftext ($this->image, 0, 8, $textcolor [0], $this->fontfile, $this->code[0]);
$this->image = imagerotate ($this->image, $degrees [0], $back);
Imagecolortransparent ($this->image, $back);
Imagecopymerge ($this->validate, $this->image, 1, 4, 4, 5, Imagesx ($this->image)-Imagesy ($this-&G T;image)-10, 100);
$this->image = Imagecreatetruecolor ($size, $size);
Imagefilledrectangle ($this->image, 0, 0, $size, $size, $back);
Imagettftext ($this->image, 0, 8, $textcolor [1], $this->fontfile, $this->code[1]);
$this->image = imagerotate ($this->image, $degrees [1], $back);
Imagecolortransparent ($this->image, $back);
Imagecopymerge ($this->validate, $this->image, 4, 4, 5, Imagesx ($this->image)-Imagesy ($this-& Gt;image)-10, 100);
$this->image = Imagecreatetruecolor ($size, $size);
Imagefilledrectangle ($this->image, 0, 0, $size-1, $size-1, $back);
Imagettftext ($this->image, 0, 8, $textcolor [2], $this->fontfile, $this->code[2]);
$this->image = imagerotate ($this->image, $degrees [2], $back);
Imagecolortransparent ($this->image, $back);
Imagecopymerge ($this->validate, $this->image, A, 4, 4, 5, Imagesx ($this->image)-Imagesy ($this-& Gt;image)-10, 100);
$this->image = Imagecreatetruecolor ($size, $size);
Imagefilledrectangle ($this->image, 0, 0, $size-1, $size-1, $back);
Imagettftext ($this->image, 0, 8, $textcolor [3], $this->fontfile, $this->code[3]);
$this->image = imagerotate ($this->image, $degrees [3], $back);
Imagecolortransparent ($this->image, $back);
Imagecopymerge ($this->validate, $this->image, 4, 4, 5, Imagesx ($this->image)-Imagesy ($this-& Gt;image)-10, 100);
Imagerectangle ($this->validate, 0, 0, $width-1, $height-1, $border);
}
/**
* Get a randomly generated verification code
* @return string Random Verification code, the returned verification code does not take any processing
*/
function GetCode ()
{
return $this->code;
}
/**
* Generate Random Code method
* @return void;
*/
protected function Randcode ()
{
$alphastr = ' abcdefghijklmnpqrstuvwxyz123456789 ';
$randstr = Array (
$ALPHASTR {rand (0)}, $alphastr {rand (0)}, $alphastr {rand (0)}, $alphastr {rand (0, 33)}
);
$this->code = strtolower ($randstr [0]. $randstr [1]. $randstr [2]. $randstr [3]);
}
/**
* RGB color to HSV color transition method
* @param $r
* @param $g
* @param $b
* @return Array HSV array
*/
protected static function Rgbtohsv ($r, $g, $b)
{
$tmp = min ($r, $g);
$min = min ($tmp, $b);
$tmp = Max ($r, $g);
$max = Max ($tmp, $b);
$v = $max;
$delta = $max-$min;
if ($max! = 0) $s = $delta/$max; S
Else
{
$s = 0;
$h = Undefinedcolor;
Return
}
if ($r = = $max) $h = ($g-$b)/$delta; Between Yellow & Magenta
else if ($g = = $max) $h = 2 + ($b-$r)/$delta; Between Cyan & Yellow
else $h = 4 + ($r-$g)/$delta; Between Magenta & Cyan
$h *= 60; Degrees
if ($h < 0) $h + = 360;
Return Array (
$h, $s, $v
);
}
/**
* The previous method has the opposite function
* @param $h
* @param $s
* @param $v
* @return Array RGB Array
*/
Protected STA Tic function Hsvtorgb ($h, $s, $v)
{
if ($s = = 0)
{
//achromatic (grey)
$r = $g = $b = $v;
return;
}
$h/=;//Sector 0 to 5
$i = Floor ($h),
$f = $h-$i;//factorial part of h
$p = $v * (1 -$s);
$q = $v * (1-$s * $f);
$t = $v * (1-$s * (1-$f));
Switch ($i)
{
Case 0:
$r = $v,
$g = $t,
$b = $p,
break,
Case 1:
$r = $q;
$ g = $v;
$b = $p;
break;
Case 2:
$r = $p,
$g = $v,
$b = $t,
Break,
Case 3:
$r = $p;
$g = $q;
$b = $v;
BR Eak
Case 4:
$r = $t,
$g = $p;
$b = $v;
Break,
Default://Case 5:
$r = $v;
$g = $p;
$b = $q;
break;
}
Return Array (
$r, $g, $b
);
}
/**
* How to use cookies to save verification codes
* @return void
*/
protected function Savecode ()
{
$code = $this->authcode ($this->code);
$this->setcookie ($code);
}
/**
* Verification Code Cookie Value acquisition method
* @return String Cookie value
*/
protected function GetCookie ()
{
if (Empty ($_cookie[$this->codecookiename]))
{
Return ';
}
Else
{
Return addslashes ($_cookie[$this->codecookiename]);
}
}
/**
* Verification Code Cookie Creation method
* @param string $code The verification code to be saved
* @return void
*/
protected function Setcookie ($code)
{
$expire = $this->codeexpire > 0? $this->codeexpire + Time (): 0;
Setcookie ($this->codecookiename, $code, $expire);
}
/**
* Verification Code encryption method
* @param string $code The random code to encrypt
* @return Mixed execution Results
*/
protected function Authcode ($code)
{
return MD5 ($code. $this->specialadd);
}
/**
* Interference Line Generation method
* @param resource $image picture resource handle
* @param string $color interference line Color
*/
protected static function Imagelinethick ($image, $color)
{
$k = rand (5, 20);
for ($px = 0; $px <; $px = $px + 1)
{
$y = $k * Sin (0.1 * ($px)); $y =200+10*sin (0.1* ($PX-200));
for ($i = 0; $i < 2; $i + +)
{
Imagesetpixel ($image, $px, $y + ten + $i, $color);
}
}
}
/**
* HTTP Header Setting method
* @return void
*/
protected static function Sendheader ()
{
Header ("Pragma:no-cache");
Header ("cache-control:max-age=1, s-maxage=1, No-cache, must-revalidate");
Header (' content-type:image/gif ');
}
}
Http://down.bKjia.c0m/down/code/php/qitayuanma/2010/1220/22330.html
http://www.bkjia.com/PHPjc/632991.html www.bkjia.com true http://www.bkjia.com/PHPjc/632991.html techarticle PHP Tutorials Generate code class codes */class SecureCode {private static $instance =null; private $code = '; private $fontfile; Private $val Idate Private $image; Private $specialadd ...