[PHP] 验证码生成类php →→→→→进入此内容的聊天室

来自 , 2021-02-04, 写在 PHP, 查看 157 次.
URL http://www.code666.cn/view/8b3bac12
  1. <?php
  2. /*
  3.         more & original php framwork
  4.         copyright (c) 2007 - 2008 ismole inc.
  5.  
  6.         $id: mooseccode.class.php 38 2008-03-19 07:39:17z aming $
  7. */
  8.  
  9. !defined('IN_MOOPHP') && exit('Access Denied');
  10.  
  11. class MooSeccode {
  12.         //note:生成的验证码
  13.         var $cecCode = '';
  14.         //note:生成的图片
  15.         var $codeImage = '';
  16.         //note:干扰素
  17.         var $disturColor = '';
  18.         //note:验证码的图片宽度
  19.         var $codeImageWidth = '80';
  20.         //note:验证码的图片高度
  21.         var $codeImageHeight  = '20';
  22.         //note:验证码位数
  23.         var $cecCodeNum = 4;
  24.        
  25.         /**
  26.          * 输出头部
  27.          *
  28.          */
  29.         function outHeader() {
  30.                 header("content-type: image/png");
  31.         }
  32.        
  33.         /**
  34.          * 生成验证码
  35.          *
  36.          */
  37.         function createCode() {
  38.                 $this->cecCode = strtoupper(substr(md5(rand()),0,$this->cecCodeNum));
  39.                 return $this->cecCode;
  40.         }
  41.  
  42.         /**
  43.          * 生成验证码图片
  44.          *
  45.          */
  46.         function createImage() {
  47.                 $this->codeImage = @imagecreate($this->codeImageWidth,$this->codeImageHeight);
  48.                 imagecolorallocate($this->codeImage, 200, 200, 200);
  49.                 return $this->codeImage;
  50.         }
  51.        
  52.         /**
  53.          * 加入图片干拢素
  54.          *
  55.          */
  56.         function setDisturbColor() {
  57.                 for ($i=0; $i<=128; $i++) {
  58.                         $this->disturColor = imagecolorallocate($this->codeImage, rand(0,255), rand(0,255), rand(0,255));
  59.                         imagesetpixel($this->codeImage,rand(2,128),rand(2,38),$this->disturColor);
  60.                 }
  61.         }
  62.  
  63.         /**
  64.          * 设置验证码图片的大小
  65.          *
  66.          * @param integer $width:
  67.          * @param integer $height:
  68.          * @return boolean;
  69.          */
  70.         function setCodeImage($width, $height) {
  71.                 if($width == '' || $height == '') { return false; }
  72.                 $this->codeImageWidth = $width;
  73.                 $this->codeImageHeight = $height;
  74.                 return true;
  75.         }
  76.  
  77.         /**
  78.          * 在图片上写入验证码
  79.          *
  80.          * @param integer $num
  81.          */
  82.         function writeCodeToImage($num = '') {
  83.                 if($num != '') {$this->cecCodeNum = $num;}
  84.                 for($i = 0; $i <= $this->cecCodeNum; $i++) {
  85.                         $bg_color = imagecolorallocate ($this->codeImage, rand(0,255), rand(0,128), rand(0,255));
  86.                         $x = floor($this->codeImageWidth / $this->cecCodeNum) * $i;
  87.                         $y = rand(0,$this->codeImageHeight - 15);
  88.                         imagechar($this->codeImage, 5, $x, $y, $this->cecCode[$i], $bg_color);
  89.                 }
  90.         }
  91.        
  92.         /**
  93.          * 把验证码的值写入session
  94.          *
  95.          * @param string $sname
  96.          */
  97.         function writeSession($sname) {
  98.                 session_start();
  99.                 session_register($sname);
  100.                 $_SESSION[$sname] = md5($this->cecCode);
  101.         }
  102.  
  103.         /**
  104.          * 输出验证码图片
  105.          *
  106.          * @param integer $width
  107.          * @param integer $height
  108.          * @param integer $num
  109.          * @param string $sname
  110.          */
  111.         function outCodeImage($width = '', $height = '' ,$num = '', $sname = 'code') {
  112.                 if($width !== '' || $height !== '') {
  113.                         $this->setCodeImage($width, $height);
  114.                 }
  115.                 $this->outHeader();
  116.                 $this->createCode();
  117.                 $this->createImage();
  118.                 $this->setDisturbColor();
  119.                 $this->writeCodeToImage($num);
  120.                 $this->writeSession($sname);
  121.                 imagepng($this->codeImage);
  122.                 imagedestroy($this->codeImage);
  123.         }
  124. }

回复 "验证码生成类php"

这儿你可以回复上面这条便签

captcha