PHP制作验证码

8/31/2015来源:PHP技巧人气:2752

php制作验证码

PHP制作验证码详细教程

效果:

myvcode.class.php:封装创建验证码的类

   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;            /*验证码字符*/
  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; $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:     /*内部私有方法设置干扰元素*/
  58:     private function setDisturbColor()
  59:     {
  60:         /*绘制干扰点*/
  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:         /*绘制干扰线*/
  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:     /*开启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: ?>

imgcode.php输出图像

   1: <?php
   2: session_start();
   3: require_once('myvcode.class.php');
   4: echo new Vcode();
   5: ?>

test.html:同过img标签引用

   1: <img src="imgcode.php">

可以加一个a标签,用js实现换一张效果:

/*局部刷新换验证码*/function changeCode(){var imgcode = document.getElementById('code');var change = document.getElementById('change');change.onclick = function(){/*必须加后面的参数才能刷新*/imgcode.src='code.php?tm'+Math.random();}}

code和change分别是img和a的id