<?php//Verification Code Class class validatecode {private $charset = ' abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789 '; //random factor private $code; //Verification Code private $codelen = 4; //Verification Code length private $width = 130; //width private $height = 50; //height private $img; //Graphics resource handle private $font ; //the specified font private $fontsize = 20; //Specify font size private $fontcolor; //Specifies font color //constructor method initialization public function __ Construct () { $this->font = root_path. ' /font/elephant.ttf '; } //Generate random Code private function createcode () { $_len = strlen ($this->charset) -1; for ($i =0; $i < $this->codelen; $i + +) { $this->code .= $this->charset[mt_ Rand (0,$_len)]; } } //Generate Background &NBSP;&NBSP;&NBSP;&NBSP;PRIVATE&NBSP;FUNCTION&NBSP;CREATEBG () { $this->img = imagecreatetruecolor ($this->width, $this->height); $color = imagecolorallocate ($this->img, mt_ Rand (157,255), mt_rand (157,255), mt_rand (157,255)); Imagefilledrectangle ($this-≫img,0, $this->height, $this->width,0, $color); } //Generate text private function createfont () { $_x = $this->width / $this->codelen; for ($i =0; $i < $this->codelen; $i + +) { $this->fontcolor = imagecolorallocate ($ This->img,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156)); imagettftext ($this->img, $this->fontsize,mt_rand ( -30,30), $_x* $i +mt_rand (1,5), $ this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]); } } //generating lines, snowflakes private function createliNE () { for ($i =0; $i <6; $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); } for ($i =0; $i <100; $i + +) { $color = imagecolorallocate ($this->img,mt_rand (200,255), Mt_ Rand (200,255), Mt_rand (200,255)); Imagestring ($this->img,mt_rand (1,5), Mt_rand (0, $this->width), Mt_rand (0, $this->height), ' * ', $color); } } //Output private Function output () { header (' Content-type:image/png '); imagepng ($this->img); imagedestroy ($this->img); } //Foreign Generation public function doimg () { $this CREATEBG (); $this->createcode (); $this->createline (); $this CreateFont (); $this->output (); } //Get Verification Code public function getcode () { Return strtolower ($this->code); }}
Nice PHP verification code class