PHP-authored image verification code class file sharing _php instance

Source: Internet
Author: User
For custom verification code classes!

<?php/* To the 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 CAPTCHA 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, Up)), 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 Verification code Public Function Create_font () {$this->create_code ();    $color = $this->font_color; $font _color = imagecolorallocate ($this->img, Hexdec (substr ($color, up)), 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-&GT;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), $    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 Verification Code 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 the code:

Generate the picture type verification code, the verification code contains the number and the capital letter, the session holds the MD5 encrypted verification code

<?php/** * Image Verification Code class * Generate image Type Verification code, verification code contains numbers and uppercase letters, the session is stored MD5 encrypted code * * Use method: * $captcha = new Catpcha (); * $captcha->buildandexportimage (); * * Author: luojing * creation time: 2013-3-27 a.m. 11:42:12 */class Captcha {private $width;//width private $height;//Height Private $codeN um;//the number of authentication code characters private $image;//Authentication Code image resource private $sessionKey; the name stored in//session private $captcha;//Authenticode string const CHARWIDTH = 10 ;//single character width, depending on the output character size/** * Create code class, initialize related parameters * @param $width picture width * @param $height picture Height * @param $codeNum number of captcha characters * @param $se The name saved in the Ssionkey session is */function __construct ($width = $codeNum, $height = 4, $sessionKey = ' captcha ') {$this-&gt ; 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 the CAPTCHA image */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-& Gt;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-&gt ; 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 border Color $border = imagecoloralLocate ($this->image, mt_rand (0, 0), Mt_rand (0, 0), Mt_rand (50));//Draw Border Imagerectangle ($this->image, 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);}}

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 ();   =floor ($width * $height/15);  if (> 240-$codeNum) {$this->disturbcolornum= 240-$codeNum;  }else{$this->disturbcolornum=;  }}//By accessing the method to output image function ShowImage ($fontFace = "") {///First step: Create Image Background $this->createimage ();  Step two: Set the interfering element $this->setdisturbcolor ();  The third step: randomly draw text $this->outputtext ($fontFace) to the image; Fourth step: Output Image $this->outputimage (); }//By calling this method to obtain a randomly created Authenticode string function Getcheckcode () {return $this->checkcode;} private Function CreateImage () {//Create image capital  SOURCE $this->image=imagecreatetruecolor ($this->width, $this->height); Random background color $backColor =imagecolorallocate ($this->image, rand (225, 255), Rand (225,255), rand (225, 255));  Fill the background with color Imagefill ($this->image, 0, 0, $backColor);  Set 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 ($  This->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); }} Private Function Createcheckcode () {//Here mainly generates random codes, starting from 2 to differentiate between 1 and l $code = "  23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ ";  $string = "; for ($i =0; $i < $this->codenum; $i + +) {$char = $code {rand (0, strlen ($Code)-1};  $string. = $char; } return $string; } Private Function Outputtext ($fontFace = "") {for ($i =0; $i < $this->codenum; $i + +) {$fontcolor =imagecolorallocate (  $this->image, rand (0, +), rand (0, +), 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:

Testing, calling the Captcha class
code.php

<?phpsession_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 the CAPTCHA to the server
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.