PHP Image Verification Code class
Buildandexportimage (); * * Author: luojing * creation time: 2013-3-27 a.m. 11:42:12 */class Captcha {private $width;//width private $height;//Height private $ codenum;//the number of authentication code characters private $image;//Authentication Code image resource private $sessionKey; the name stored in//session private $captcha;//CAPTCHA String const Charwidth = 10;//A single character width, depending on the output character size/** * Create CAPTCHA class, initialize related parameters * @param $width picture Width * @param $height Image Height * @param $codeNum Verification Code Number of characters * @param the name saved in $sessionKey session */function __construct ($width =, $height =, $codeNum = 4, $sessionKey = ' CA Ptcha ') {$this->width = $width; $this->height = $height; $this->codenum = $codeNum; $this->sessionkey = $ sessionkey;//Guaranteed Minimum height and width if ($height <) {$this->height = 20;} if ($width < ($codeNum * self::charwidth + 10)) {//left 5 pixels gap $this->width = $codeNum * self::charwidth + 10;} /** * Constructs and outputs a CAPTCHA image */public function buildandexportimage () {$this->createimage (); $this->setdisturb (); $this Setcaptcha (); $this->exportimage ();} /** * Construct image, set background */private function CreateImage () {//create diagramLike $this->image = Imagecreatetruecolor ($this->width, $this->height); Create background color $BG = imagecolorallocate ($this->image, Mt_rand (up, 255), Mt_rand (up, 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 interference point for ($i = 0; $i <; $i + +) {$color = Imagecolorallocate ($this-> ; image, Mt_rand (Mt_rand, Max), Mt_rand (->image), Imagesetpixel (5, Mt_rand (+), $this WIDTH-10), Mt_rand (5, $this->height-3), $color);} Set the interference line for ($i = 0; $i < $i + +) {$color = Imagecolorallocate ($this->image, Mt_rand (), Mt_rand (), M T_rand, Imagearc ($this->image, Mt_rand ( -10, $this->width), Mt_rand ( -10, $this->height), Mt_rand ( (+), Mt_rand, (+), $color);} Create a border color $border = imagecolorallocate ($this->image, mt_rand (0, 0), Mt_rand (0, 50), Mt_rand (+/-); Draw Border Imagerectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $border);} /** * Generate and draw Verification code */private function Setcaptcha () {$str = ' 23456789ABCDEFGHJKLMNPQRSTUVWXYZ ';//Generate CAPTCHA character for ($i = 0; $i < $th is->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_rand (0, 200) , Mt_rand (0, +), Mt_rand (0, $)), $x = Floor (($this->width-10)/$this->codenum), $x = $x * $i + Floor (($x-self::char Width)/2) + 5; $y = Mt_rand (2, $this->height-20); Imagechar ($this->image, 5, $x, $y, $this->captcha{$i}, $color); }}/* * Output image, verification code saved to session */private function Exportimage () {if (Imagetypes () & Img_gif) {header (' Content-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);d ie ("Don t support image type!");} Save the CAPTCHA information to the SESSION, MD5 encryption 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);}}