This example for you to share the PHP code class, the use of objects to achieve the authentication code class for your reference, the specific contents are as follows
<?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, 1
), 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 Disturbing line publicfunction 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), $
Pix_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); }
}
Effect Chart:
Wonderful topic sharing: ASP.net verification Code daquan PHP Verification Code daquan Java Verification Code Daquan
The above is the use of object-written verification code class of all the content, I hope that you learn PHP program help.