PHP generates code class codes for validation code

Source: Internet
Author: User
Tags min rand setcookie sin

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
   }

   /**
     * Specifies the path to the font file, which defaults to the current folder under Arial.ttf file
      * @param $fontfile file path
     * @return void
     */
 &nbs p;  function Loadfont ($fontfile)
    {
        $this- >fontfile = $fontfile;
   }

/**
* Image output method, before executing this method the program should not have any form of output
* @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->savecode ();


Imagegif ($this->validate, $filename);


Imagedestroy ($this->validate);


Imagedestroy ($this->image);


}





/**


* Verification Code Verification method


* @param $input The string to be validated, that is, what the user is entering


* @return Boolean validation results


*/


function Verify ($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,), rand (1), $randpixelcolor);


}





$temp = SELF::RGBTOHSV (rand, 255), Rand (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 is 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, 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 the randomly generated verification code


* @return String Random Authentication code, the returned verification code does not do 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 the HSV color conversion method


* @param $r


* @param $g


* @param $b


* @return Array HSV arrays


*/


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 same method functions the opposite


* @param $h


* @param $s


* @param $v


* @return Array RGB


*/


protected static function Hsvtorgb ($h, $s, $v)


{


if ($s = = 0)


{


Achromatic (Grey)


$r = $g = $b = $v;


Return


}





$h/= 60; 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;


Break


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 save the authentication code using cookies
* @return void
*/
protected function Savecode ()
{
$code = $this->authcode ($this->code);
$this->setcookie ($code);
}

/**


* Authentication Code Cookie Value acquisition method


* @return String Cookie value


*/


protected function GetCookie ()


{


if (Empty ($_cookie[$this-&gt;codecookiename]))


{


Return ";


}


Else


{


Return addslashes ($_cookie[$this-&gt;codecookiename]);


}


}





/**


* Verification Code Cookie Creation method


* @param string $code The authentication code to be saved


* @return void


*/


protected function Setcookie ($code)


{


$expire = $this-&gt;codeexpire &gt; 0? $this-&gt;codeexpire + Time (): 0;


Setcookie ($this-&gt;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-&gt;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 &lt; $px = $px + 1)


{


$y = $k * Sin (0.1 * ($px)); $y =200+10*sin (0.1* ($PX-200));


for ($i = 0; $i &lt; 2; $i + +)


{


Imagesetpixel ($image, $px, $y + + $i, $color);


}





}


}

/**
* HTTP Header Setup 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.111cn.net/down/code/php/qitayuanma/2010/1220/22330.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.