PHP Chinese character Verification Code generation program (1/3) _php Tutorial

Source: Internet
Author: User
PHP Chinese character Verification Code generation program This is the PHP Chinese character verification code generator is very simple, the generation of Chinese character verification code needs to support the font, the following we use the ARIAL.TTF to support the generation, otherwise generated Chinese image verification code will be garbled.

PHP Tutorial Chinese character Verification Code generation program
The PHP Chinese character Verification Code generation program is actually very simple, the generation of Chinese character verification code needs to support the font, the following we use the ARIAL.TTF to support the generation, otherwise generated Chinese image verification code will be garbled.
*/

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

1 2 3

http://www.bkjia.com/PHPjc/633019.html www.bkjia.com true http://www.bkjia.com/PHPjc/633019.html techarticle PHP Chinese character Verification Code generation program This is about the PHP Chinese character verification code generator is very simple, the generation of Chinese character verification code needs to support the font, the following we use AR ...

  • 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.