PHP dynamic random Generation Verification code class Code _php skill

Source: Internet
Author: User
Tags imagejpeg rand
Here is the effect chart, which is an effect diagram without opening the jamming code.

The following is the class code

Copy Code code as follows:

<?php
/************************************************
File:imagecode
Done: Generate dynamic Authentication code class
DATE "2010-3-31
author:www.5dkx.com 5D Happy Blog
************************************************************************/
Class imagecode{
Private $width; Verify Code picture width
Private $height; Verify Code picture Height
Private $codeNum; Number of verification code characters
Private $checkCode; Verify Code characters
Private $image; Verification Code Canvas
/************************************************************************
Function: Constructors
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 back interference code fails.
/* If you want to use the interference code, the following * *
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;//uppercase letters
Case 2: $rand _number = rand (97,122); break;//lowercase Letters
}
$ASC = sprintf ("%c", $rand _number);
$ASC _number = $asc _number. $asc;
}
return $ASC _number;
}
Private Function Setdisturbcolor ()//interference? set
{
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 ();
*******************************************************************/
?>
Related Article

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.