Write a validation code class in PHP and encapsulate it.
class Name: validationcode.class.php
The code is as follows:
Copy Code code as follows:
<?php
Class Validationcode {
Private $width;
Private $height;
Private $codeNum;
Private $image; Image Resources
Private $disturbColorNum;
Private $checkCode;
function __construct ($width =80, $height =20, $codeNum =4) {
$this->width= $width;
$this->height= $height;
$this->codenum= $codeNum;
$this->checkcode= $this->createcheckcode ();
$number =floor ($width * $height/15);
if ($number > 240-$codeNum) {
$this->disturbcolornum= 240-$codeNum;
}else{
$this->disturbcolornum= $number;
}
}
To output an image to a browser by accessing this method
function ShowImage ($fontFace = "") {
First step: Create an image background
$this->createimage ();
Step two: Set the jamming elements
$this->setdisturbcolor ();
Step three: Draw the text randomly to the image
$this->outputtext ($fontFace);
Fourth Step: Output image
$this->outputimage ();
}
Obtain a randomly created authentication code string by calling this method
function Getcheckcode () {
return $this->checkcode;
}
Private Function CreateImage () {
Create an image resource
$this->image=imagecreatetruecolor ($this->width, $this->height);
Random background color
$backColor =imagecolorallocate ($this->image, rand (255), Rand (225,255), rand (225, 255));
Add color to Background
Imagefill ($this->image, 0, 0, $backColor);
Set Border color
$border =imagecolorallocate ($this->image, 0, 0, 0);
Draw a rectangular border
Imagerectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $border);
}
Private Function Setdisturbcolor () {
For ($i =0 $i < $this->disturbcolornum; $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);
}
For ($i =0 $i <10; $i + +) {
$color =imagecolorallocate ($this->image, rand (255), Rand (255), rand (200, 255));
Imagearc ($this->image, rand ( -10, $this->width), rand ( -10, $this->height), rand (+), rand (20, 200), 55, 44, $color);
}
}
Private Function Createcheckcode () {
Here the main generated random code, starting from 2 is to differentiate between 1 and L
$code = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
$string = ';
For ($i =0 $i < $this->codenum; $i + +) {
$char = $code {rand (0, strlen ($code)-1)};
$string. = $char;
}
return $string;
}
Private Function outputtext ($fontFace = "") {
For ($i =0 $i < $this->codenum; $i + +) {
$fontcolor =imagecolorallocate ($this->image, rand (0, 128), rand (0, 128), rand (0, 128));
if ($fontFace = = "") {
$fontsize =rand (3, 5);
$x =floor ($this->width/$this->codenum) * $i +3;
$y =rand (0, $this->height-15);
Imagechar ($this->image, $fontsize, $x, $y, $this->checkcode{$i}, $fontcolor);
}else{
$fontsize =rand (12, 16);
$x =floor (($this->width-8)/$this->codenum) * $i +8;
$y =rand ($fontSize +5, $this->height);
Imagettftext ($this->image, $fontsize, Rand ( -30), $x, $y, $fontcolor, $fontFace, $this->checkcode{$i});
}
}
}
Private Function Outputimage () {
if (Imagetypes () & Img_gif) {
Header ("Content-type:image/gif");
Imagepng ($this->image);
}else if (Imagetypes () & img_jpg) {
Header ("Content-type:image/jpeg");
Imagepng ($this->image);
}else if (Imagetypes () & Img_png) {
Header ("Content-type:image/png");
Imagepng ($this->image);
}else if (Imagetypes () & Img_wbmp) {
Header ("Content-type:image/vnd.wap.wbmp");
Imagepng ($this->image);
}else{
Die ("PHP does not support image creation");
}
}
function __destruct () {
Imagedestroy ($this->image);
}
}
Use the following:
test, calling the validation code class
code.php
Copy Code code as follows:
<?php
Session_Start ();
Include "validationcode.class.php";
$code =new Validationcode (80, 20, 4);
$code->showimage (); Output to a page for registration or logon use
$_session["code"]= $code->getcheckcode (); To save the Authenticode to the server