-
-
/**
- * Universal verification code class img.php
- * Version: V0.1
- * bbs.it-home.org 2013/3/1
- */
- class ValidateCode {
- private $charset="abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //Random factor
- private $code; //Verification code text
- codelen= 4; //Verification code displays several words
- private $width=130; //Verification code width
- private $height=50; //Verification code height
- private $img; //Verification code resource handle
- private $font; //Specified font
- private $fontsize=20; //Specified font size
- private $fontcolor; //Random font color
-
- //Construction class to write font
- public function __construct(){
- $this->font =ROOT_PATH.'/font/elephant.ttf';
- }
- //Create 4 random codes
- private function createCode(){
- $_leng=strlen($this->charset);
- for($i=1 ;$icodelen;$i++){
- $this->code.=$this->charset[mt_rand(0,$_leng)];
- }
- return $this-> code;
- }
-
- //Create a background
- private function createBg(){
- //Create a canvas for a resource jubing
- $this->img=imagecreatetruecolor($this->width,$this->height) ;
- //Background color
- $color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
- //Draw a rectangle
- imagefilledrectangle($this-> img,0,$this->height,$this->width,0,$color);
- }
-
- //Create font
- private function createFont(){
- $_x=($this->width / $this->codelen); //Font length
- for ($i=0;$icodelen;$i++){
- //Text color
- $color=imagecolorallocate($this-> ;img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
- //Resource handle font size tilt font length font height font color font specific text
- imagettftext($this->img,$this- >fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$color,$this->font,$this->code [$i]);
- }
- }
- //Random lines
- private function createLine(){
- //Random lines
- for ($i=0;$i $color = imagecolorallocate( $this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
- imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$ this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
- }
- //Random snowflakes
- for ($i=0; $i $color = imagecolorallocate($this->img,mt_rand(220,255),mt_rand(220,255),mt_rand(220,255));
- imagestring($this->img,mt_rand( 1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
- }
- }
- //Output background
- private function outPut (){
- //Generate header
- header('ContentType:img/png');
- //Output image
- imagepng($this->img);
- //Destroy the result set
- imagedestroy($this-> ;img);
- }
- //External output
- public function doimg(){
- //Load background
- $this->createBg();
- //Load file
- $this->createCode();
- / /Load line
- $this->createLine();
- //Load font
- $this->createFont();
- //Load background
- $this->outPut();
- }
-
- //Get Verification code
- public function getCode(){
- return strtolower($this->code);
- }
-
- }
- ?>
Copy code
Call example: index.php
I recommend several articles about PHP verification codes to everyone:
php random verification code php generates random verification code (picture and text)
Use php to generate verification code with snowflake background
A verification code written in php
php code to generate dynamic image verification code
|