/**
* Verification Code Class
* chaojie2008@126.com
* 2012-02-09
* */
Class Vailimg {
Private $width; Verify the width of the code picture
Private $height; Verify the height of the code picture
Private $codeNum; Number of verification code characters
Private $checkCode; Verification Code characters
Private $image; Verification Code Canvas
/* The constructor method is used to instantiate the Captcha object, and for some member properties to be initialized */
/* Parameter width: Set the size of the CAPTCHA picture, the default width value is 60 pixels */
/* Parameter height: Set the size of the CAPTCHA picture, the default height value is 20 pixels */
/* Parameter codenum: Set the number of letters and numbers in the verification code, the default number is 4 */
function __construct ($width =60, $height =20, $codeNum =4) {
$this->width= $width; For the member attribute width at the beginning of the
$this->height= $height; For a member property, the height of the first
$this->codenum= $codeNum; Codenum for the member property
$this->checkcode= $this->createcheckcode (); Checkcode for the member property
}
function ShowImage () {//output an image to the browser by accessing the method
$this->getcreateimage (); Call an internal method to create a canvas and make it primary
$this->outputtext (); Outputting random strings to an image
$this->setdisturbcolor (); Set some interfering pixels into the image
$this->outputimage (); Generate an image of the appropriate format and output
}
function Getcheckcode () {//Access this method to get a randomly created captcha string
return $this->checkcode; Returns the member property $checkcode the saved string
}
Private Function Getcreateimage () {//used to create an image resource, and initially make the back
$this->image=imagecreate ($this->width, $this->height);
$back =imagecolorallocate ($this->image, 255, 255, 255);
$border =imagecolorallocate ($this->image, 0, 0, 0);
Imagerectangle ($this->image,0,0, $this->width-1, $this->height-1, $border);
}
Private Function Createcheckcode () {//randomly generate a user-specified number of strings
for ($i =0; $i < $this->codenum; $i +) {
$number =rand (0,2);
Switch ($number) {
Case 0: $rand _number=rand (48,57); Digital
Case 1: $rand _number=rand (65,90); Capital
Case 2: $rand _number=rand (97,122); lowercase letters
}
$ascii =sprintf ("%c", $rand _number);
$ascii _number= $ascii _number. $ascii;
}
return $ascii _number;
}
Private Function Setdisturbcolor () {//Set interference pixels, output 100 points of different colors to the image
for ($i =0; $i <=50; $i + +) {
$color = Imagecolorallocate ($this->image, rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($this->image,rand (1, $this->width-2), rand (1, $this->height-2), $color);
}
}
Private Function Outputtext () {//random color, random placement, random string output to image
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 () {//auto-detect GD supported image types and output images
if (Imagetypes () & Img_gif) {//Determine if the function that generated the GIF format image exists
Header ("Content-type:image/gif"); Send header information set MIME type to Image/gif
Imagegif ($this->image); Output an image to a browser in GIF format
}elseif (Imagetypes () & img_jpg) {//Determine if the function that generated the JPG format image exists
Header ("Content-type:image/jpeg"); Send header information set MIME type to Image/jpeg
Imagejpeg ($this->image, "", 0.5); Output an image to a browser in jpen format
}elseif (Imagetypes () & Img_png) {//Determine if the function that generated the PNG format image exists
Header ("Content-type:image/png"); Send header information set MIME type to Image/png
Imagepng ($this->image); Export an image to a browser in PNG format
}elseif (Imagetypes () & img_wbmp) {//Determine if the function that generated the WBMP format image exists
Header ("Content-type:image/vnd.wap.wbmp"); Send header to Image/wbmp
Imagewbmp ($this->image); Output an image to a browser in wbmp format
}else{//If there are no supported image types
Die ("PHP does not support image creation! "); Do not output an image, output an error message, and exit the program
}
}
function __destruct () {//Destroy the image resource to free memory before the object ends
Imagedestroy ($this->image); Call the method in the GD library to destroy the image resource
}
}
?>
Excerpt from chaojie2009 's column
http://www.bkjia.com/PHPjc/478390.html www.bkjia.com true http://www.bkjia.com/PHPjc/478390.html techarticle PHP/** * Verification Code class * Chaojie2008@126.com * 2012-02-09 * */class Vailimg {private $width;//CAPTCHA image width private $height; Verify the height of the code picture private $codeN ...