PHP Verification Code

Source: Internet
Author: User
1 :? Php2: * 3: * file: myvcode. class. php4: * Verification Code Class, Class Name Vcode5: * 6: classVcode7: {8: private $ width; * Verification Code width * 9: private $ height; * Verification Code height * 10: private $ codeNum; * Number of Verification Code characters * 11: private $ checkCode; * Verification Code characters *

1 :? Php 2:/* 3: * file: myvcode. class. php 4: * Verification Code class, class Name Vcode 5: */6: class Vcode 7: {8: private $ width;/* Verification Code width */9: private $ height; /* Verification Code height */10: private $ codeNum;/* Number of Verification Code characters */11: private $ checkCode;/* Verification Code character *

1:
 
   2: /*
   3: * file:myvcode.class.php
4: * Verification Code Class, Class Name Vcode
   5: */
   6: class Vcode
   7: {
8: private $ width;/* Verification Code width */
9: private $ height;/* Verification Code height */
10: private $ codeNum;/* number of characters in the Verification Code */
11: private $ checkCode;/* Verification Code character */
12: private $ image;/* Verification Code resource */
13: private $ pixNum;/* Number of interference points to be drawn */
14: private $ lineNum;/* number of lines that draw interference lines */
  15:
  16:     /*
17: * constructor instantiate the verification code object and initialize data
18: * @ param int $ width: Set the default width.
19: * @ param int $ height: Set the default height.
20: * @ param int $ codeNum: set the number of characters in the verification code
21: * @ param int $ pixNum: set the number of interference points.
22: * @ param int $ lineNum: set the number of interference lines.
  23:     */
  24:     function __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)
  25:     {
  26:         $this->width = $width;
  27:         $this->height = $height;
  28:         $this->codeNum = $codeNum;
  29:         $this->pixNum = $pixNum;
  30:         $this->lineNum = $lineNum;
  31:     }
32:/* internal private method, creating image resources */
  33:     private function getCreateImage()
  34:     {
  35:         $this->image = imagecreatetruecolor($this->width, $this->height);
  36:         $white = imagecolorallocate($this->image,0xff,0xff,0xff);
  37:         imagefill($this->image, 0, 0, $white);
  38:         $black = imagecolorallocate($this->image,0,0,0);
  39:         imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);
  40:     }
41:/* internal private method, draw characters, remove o0Llz and 012 */
  42:     private function createCheckCode()
  43:     {
  44:         $code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';
  45:         $this->checkCode = "";
  46:         for($i=0; $i<$this->codeNum;$i++)
  47:         {
  48:             $char = $code{rand(0,strlen($code) - 1)};
  49:             $this->checkCode .= $char;
  50:             $fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));
  51:             $fontSize = rand(3,5);
  52:             $x = rand(0,$this->width-imagefontwidth($fontSize));
  53:             $y = rand(0,$this->height-imagefontheight($fontSize));
  54:             imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);
  55:         }
  56:     }
57:/* Set interference elements in the internal private Method */
  58:     private function setDisturbColor()
  59:     {
60:/* draw interference points */
  61:         for($i=0; $i<$this->pixNum; $i++)
  62:         {
  63:             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
  64:             imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
  65:         }
66:/* draw interference lines */
  67:         for($i=0; $i<$this->lineNum; $i++)
  68:         {
  69:             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));

70: imageline ($ this-> image, rand (1, $ this-> width/2), rand (1, $ this-> height/2 ),

Rand ($ this-> width/2, $ this-> width-2), rand ($ this-> height/2, $ this-> height-2 ), $ color );

  71:         }
  72:     }
73:/* enable session storage and use echo to output the image */
  74:     function __toString()
  75:     {
  76:         $_SESSION['code'] = strtoupper($this->checkCode);
  77:         $this->getCreateImage();
  78:         $this->createCheckCode();
  79:         $this->setDisturbColor();
  80:         $this->outputImg();
  81:     }
82:/* internal private method output image */
  83:     private function outputImg()
  84:     {
  85:         header("content-type:image/png");
  86:         imagepng($this->image);
  87:     }
88:/* destructor, release object */
  89:     function __destruct()
  90:     {
  91:         imagedestroy($this->image);
  92:     }
  93: }
  94: ?>

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.