The
Applies to custom validation code classes!
<?php * * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools |
Templates * and open the template in the editor.
* * Class image{private $img;
Public $width = 85;
Public $height = 25;
Public $code;
Public $code _len = 4;
Public $code _str = "329832983DSDSKDSLKQWEWQ2LKFDSFSDJFDSFDSJWLKFJ93290KFDSKJFDSOIDSLK";
Public $BG _color = ' #DCDCDC ';
Public $font _size = 16;
Public $font = ' Font.ttf ';
Public $font _color = ' #000000 ';
Create a validation code hungry character create Public function Create_code () {$code = ';
For ($i =0 $i < $this->code_len; $i + +) {$code. = $this->code_str[mt_rand (0, strlen ($this->code_str)-1)];
return $this->code = $code;
//Output image Public Function GetImage () {$w = $this->width;
$h = $this->height;
$BG _color = $this->bg_color;
$img = Imagecreatetruecolor ($w, $h); $BG _color = imagecolorallocate ($img, Hexdec (substr ($BG _coloR, 1,2)), Hexdec (substr ($BG _color, 3,2)), Hexdec (substr ($BG _color, 5,2)));
Imagefill ($img, 0, 0, $bg _color);
$this->img = $img;
$this->create_font ();
$this->create_pix ();
$this->show_code ();
//write authentication code public Function Create_font () {$this->create_code ();
$color = $this->font_color; $font _color = imagecolorallocate ($this->img, Hexdec (substr ($color, 1,2)), Hexdec (substr ($color, 3,2)), Hexdec (
substr ($color, 5,2));
$x = $this->width/$this->code_len; For ($i =0 $i < $this->code_len; $i + +) {$txt _color = imagecolorallocate ($this->img, Mt_rand (0,100), Mt_rand (0,
), Mt_rand (0, 200)); Imagettftext ($this->img, $this->font_size, Mt_rand ( -30), $x * $i +mt_rand (3, 6), Mt_rand ($this->height/1.2
, $this->height), $txt _color, $this->font, $this->code[$i]); Imagestring ($this->img, $this->font_size, $x * $i +mt_rand (3, 6), Mt_rand (0, $this->HEIGHT/4), $this-> code[$i], $font _colOR);
} $this->font_color = $font _color;
//Draw Interference line public function Create_pix () {$pix _color= $this->font_color; For ($i =0 $i <100; $i + +) {Imagesetpixel ($this->img, Mt_rand (0, $this->width), Mt_rand (0, $this->height), $p
Ix_color);
For ($j =0 $j <4; $j + +) {imagesetthickness ($this->img, Mt_rand (1, 2)); Imageline ($this->img, Mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_
Rand (0, $this->height), $pix _color);
}//Get Authenticode Public Function GetCode () {return strtoupper ($this->code);
//Output Verification code Private Function Show_code () {Header ("content-type:image/png");
Imagepng ($this->img);
Imagedestroy ($this->img);
}
}
Let's look at one more example of code:
Generate the picture type verification code, the verification code contains the number and the capital letter, in session holds the MD5 encryption the authentication code
<?php/** * Image Verification Code class * Generate a picture type verification code, the verification code contains numbers and uppercase letters, the session in the storage MD5 encrypted verification code * * Use method: * $captcha = new Catpcha ();
* $captcha->buildandexportimage (); * Author: luojing * Create time: 2013-3-27 morning 11:42:12/class Captcha {private $width;//width private $height;//High PR Ivate $codeNum//The number of authentication code characters private $image//Authentication code image Resources Private $sessionKey//session The name of the saved in the private $captcha;/Verify Code String const Charwidth = 10;//A single character width, depending on the size of the output character/** * Create a validation code class, initialize the relevant parameters * @param $width picture width * @param $height picture Height * @param $co Denum Verification Code character number * @param $sessionKey the name of the session is saved/function __construct ($width =, $height =, $codeNum = 4, $se
Ssionkey = ' Captcha ') {$this->width = $width;
$this->height = $height;
$this->codenum = $codeNum;
$this->sessionkey = $sessionKey;
Ensure minimum height and width if ($height <) {$this->height = 20;
} if ($width < ($codeNum * self::charwidth + 10)) {//left or right reserved 5 pixel voids $this->width = $codeNum * self::charwidth + 10;
}
}
/*** Construct and output the verification code picture/Public Function buildandexportimage () {$this->createimage ();
$this->setdisturb ();
$this->setcaptcha ();
$this->exportimage (); /** * Construct image, set background/Private function createimage () {//create image $this->image = Imagecreatetruecolor ($this->
width, $this->height);
Create a background color $BG = imagecolorallocate ($this->image, Mt_rand (255), Mt_rand (the 255), Mt_rand (220, 255));
Fill background color imagefilledrectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $BG); /** * Set interference element/Private function Setdisturb () {//Set the noise point for ($i = 0; $i <; $i + +) {$color = Imag
Ecolorallocate ($this->image, Mt_rand, Mt_rand), Mt_rand (150, 200));
Imagesetpixel ($this->image, Mt_rand (5, $this->width-10), Mt_rand (5, $this->height-3), $color); //Set interference line for ($i = 0; $i < $i + +) {$color = Imagecolorallocate ($this->image, Mt_rand (), Mt_ran D (Mt_ran),D (150, 220)); Imagearc ($this->image, Mt_rand ( -10, $this->width), Mt_rand ( -10, $this->height), Mt_rand (at), Mt_rand (20
, $color);
//Create border color $border = imagecolorallocate ($this->image, Mt_rand (0), Mt_rand (0), Mt_rand (0, 50));
Draw a border Imagerectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $border);
/** * Generate and draw verification code/Private Function Setcaptcha () {$str = ' 23456789ABCDEFGHJKLMNPQRSTUVWXYZ ';
Generates a CAPTCHA character for ($i = 0; $i < $this->codenum; $i + +) {$this->captcha. = $str {mt_rand (0, strlen ($str)-1)}; //Draw Verification code for ($i = 0; $i < strlen ($this->captcha); $i + +) {$color = Imagecolorallocate ($this->image, Mt_ra
nd (0), Mt_rand (0), Mt_rand (0, 200));
$x = Floor ($this->width-10)/$this->codenum);
$x = $x * $i + Floor (($x-self::charwidth)/2) + 5;
$y = Mt_rand (2, $this->height-20);
Imagechar ($this->image, 5, $x, $y, $this->captcha{$i}, $color);
} * * * Output image, verify code saved to session * * Private Function Exportimage () {if (Imagetypes () & Img_gif) {header (' Conte
Nt-type:image/gif ');
Imagegif ($this->image);
else if (Imagetypes () & Img_png) {header (' content-type:image/png ');
Imagepng ($this->iamge);
else if (Imagetypes () & Img_jpeg) {header (' content-type:image/jpeg ');
Imagepng ($this->iamge);
else {Imagedestroy ($this->image);
Die ("Don ' t support image type!");
///Save Authenticode information to session, MD5 encrypt if (!isset ($_session)) {session_start ();
} $_session[$this->sessionkey] = MD5 ($this->captcha);
Imagedestroy ($this->image);
function __destruct () {unset ($this->width, $this->height, $this->codenum, $this->captcha); }
}
Example three:
<?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 the image function ShowImage ($fontFace = "") to the browser by accessing this method {//Step one: Create Image background $this->createimage ();
Step two: Set interference element $this->setdisturbcolor ();
Step three: Randomly draw text $this->outputtext ($fontFace) to the image;
Fourth step: Output Image $this->outputimage ();
///By calling this method to obtain a randomly created authentication code string function Getcheckcode () {return $this->checkcode;
Private Function CreateImage () {//Create 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 the background Imagefill ($this->image, 0, 0, $backColor);
Sets the 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 ($th
Is->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); The Private Function Createcheckcode () {//is the main generating random code, starting from 2 to differentiate between 1 and l $code = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKM
NPQRSTUVWXYZ ";
$string = '; for ($i =0; $i < $this->codenum;
$i + +) {$char = $code {rand (0, strlen ($code)-1)};
$string. = $char;
return $string; The 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
<?php
session_start ();
Include "validationcode.class.php";
$code =new Validationcode (4);
$code->showimage (); Output to the page for registration or login using
$_session["code"]= $code->getcheckcode ();//save Authenticode to server