PHP dynamic random Generation Verification Code class Code _php tutorial

Source: Internet
Author: User
Tags imagejpeg
Here's the one that doesn't have a jamming code.

The following is the class code

Copy CodeThe code is as follows:
/************************************************
File:imagecode
Done: Generate Dynamic Captcha class
DATE "2010-3-31
author:www.5dkx.com 5D Happy Blog
************************************************************************/
Class imagecode{
Private $width; Verification Code Picture width
Private $height; Verification Code Picture Height
Private $codeNum; Number of verification code characters
Private $checkCode; Verification Code characters
Private $image; Verification Code Canvas
/************************************************************************
Function: Constructor
Done: Member Property initialization
author:www.5dkx.com 5D Happy Blog
************************************************************************/
function __construct ($width =60, $height =20, $codeNum =4)
{
$this->width = $width;
$this->height = $height;
$this->codenum = $codeNum;
$this->checkcode = $this->createcheckcode ();
}
function ShowImage ()
{
$this->getcreateimage ();
$this->outputtext ();
$this->setdisturbcolor ();
$this->outputimage ();
}
function Getcheckcode ()
{
return $this->chekcode;
}
Private Function Getcreateimage ()
{
$this->image = Imagecreatetruecolor ($this->width, $this->height);
$back = Imagecolorallocate ($this->image,255,255,255);
$border = Imagecolorallocate ($this->image,255,255,255);
Imagefilledrectangle ($this->image,0,0, $this->width-1, $this->height-1, $border);
Use pure white to fill the rectangular box, where the noise code after the failure
/* Use the following * * If you want to use interference codes.
Imagerectangle ($this->image,0,0, $this->width-1, $this->height-1, $border);
}
Private Function Createcheckcode ()
{
for ($i =0; $i < $this->codenum; $i + +)
{
$number = rand (0,2);
Switch ($number)
{
Case 0: $rand _number = rand (48,57); break;//Digital
Case 1: $rand _number = rand (65,90); break;//Capital Letter
Case 2: $rand _number = rand (97,122); break;//lowercase Letter
}
$ASC = sprintf ("%c", $rand _number);
$ASC _number = $asc _number. $asc;
}
return $ASC _number;
}
Private Function Setdisturbcolor ()//interference setting
{
for ($i =0; $i <=100; $i + +)
{
$color = Imagecolorallocate ($this->image,rand (0,255), Rand (0,255), Rand (0,255));
$color = Imagecolorallocate ($this->image,255,255,255);
Imagesetpixel ($this->image,rand (1, $this->width-2), rand (1, $this->height-2), $color);
}
$color = Imagecolorallocate ($this->image,0,0,0);
Imagesetpixel ($this->image,rand (1, $this->width-2), rand (1, $this->height-2), $color);
}
Private Function Outputtext ()
{
Random color, random placement, random string to image output
for ($i =0; $i <= $this->codenum; $i + +)
{
$BG _color = imagecolorallocate ($this->image,rand (0,255), Rand (0,128), Rand (0,255));
$x = Floor ($this->width/$this->codenum) * $i +3;
$y = rand (0, $this->height-15);
Imagechar ($this->image,5, $x, $y, $this->checkcode[$i], $bg _color);
}
}
Private Function Outputimage ()
{
if (Imagetypes () &img_gif)
{
Header ("Content_type:image/gif");
Imagegif ($this->image);
}
ElseIf (Imagetypes () &img_jpg)
{
Header ("Content-type:image/jpeg");
Imagejpeg ($this->image, "", 0.5);
}
ElseIf (Imagetypes () &img_png)
{
Header ("Content-type:image/png");
Imagejpeg ($this->image);
}
ElseIf (Imagetypes () &img_wbmp)
{
Header ("Content-type:image/vnd.wap.wbmp");
Imagejpeg ($this->image);
}
Else
{
Die ("PHP does not support image creation");
}
}
function __destruct ()
{
Imagedestroy ($this->image);
}
}
/* Display */
/*******************************************************************
Session_Start ();
$image = new Imagecode (60,20,4);
$image->showimage ();
$_session[' imagecode '] = $image->getcheckcode ();
*******************************************************************/
?>

http://www.bkjia.com/PHPjc/321592.html www.bkjia.com true http://www.bkjia.com/PHPjc/321592.html techarticle here is, this is no open interference code below is the class Code copy code code as follows:? php/************************************************//file: ...

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