欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP制作验证码

程序员文章站 2022-05-30 18:45:29
...

1: ?php 2: /* 3: * file:myvcode.class.php 4: * 验证码类,类名Vcode 5: */ 6: class Vcode 7: { 8: private $width; /*验证码宽度*/ 9: private $height; /*验证码高度*/ 10: private $codeNum; /*验证码字符个数*/ 11: private $checkCode; /*验证码字符*

   1: 
   2: /*
   3: * file:myvcode.class.php
   4: * 验证码类,类名Vcode
   5: */
   6: class Vcode
   7: {
   8:     private $width;              /*验证码宽度*/
   9:     private $height;             /*验证码高度*/
  10:     private $codeNum;            /*验证码字符个数*/
  11:     private $checkCode;            /*验证码字符*/
  12:     private $image;                /*验证码资源*/
  13:     private $pixNum;            /*绘制干扰点的个数*/
  14:     private $lineNum;            /*绘制干扰线的条数*/
  15:
  16:     /*
  17:     *构造方法实例化验证码对象,并初始化数据
  18:     *@param int $width         设置默认宽度
  19:     *@param int $height     设置默认高度
  20:     *@param int $codeNum    设置验证码中的字符个数
  21:     *@param int $pixNum        设置干扰点的个数
  22:     *@param int $lineNum    设置干扰线的数量
  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:     /*内部私有方法,创建图像资源*/
  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:     /*内部私有方法,绘制字符,去掉o0Llz和012*/
  42:     private function createCheckCode()
  43:     {
  44:         $code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';
  45:         $this->checkCode = "";
  46:         for($i=0; $icodeNum;$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:     /*内部私有方法设置干扰元素*/
  58:     private function setDisturbColor()
  59:     {
  60:         /*绘制干扰点*/
  61:         for($i=0; $ipixNum; $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:         /*绘制干扰线*/
  67:         for($i=0; $ilineNum; $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:     /*开启session保存 利用echo 输出图像*/
  74:     function __toString()
  75:     {
  76:         $_SESSION['code'] = strtoupper($this->checkCode);
  77:         $this->getCreateImage();
  78:         $this->createCheckCode();
  79:         $this->setDisturbColor();
  80:         $this->outputImg();
  81:     }
  82:     /*内部私有方法输出图像*/
  83:     private function outputImg()
  84:     {
  85:         header("content-type:image/png");
  86:         imagepng($this->image);
  87:     }
  88:     /*析构方法,释放对象*/
  89:     function __destruct()
  90:     {
  91:         imagedestroy($this->image);
  92:     }
  93: }
  94: ?>