• PHP动态生成验证码


    1 <?php
      2 /************************************************************************
      3 //FILE:ImageCode
      4 //DONE:生成动态验证码类
      5 //DATE"2010-3-31
      6 //Author:www.5dkx.com 5D开心博客
      7 ************************************************************************/
      8 class ImageCode{
      9   private $width;           //验证码图片宽度
    10    private $height;         //验证码图片高度
    11    private $codeNum;       //验证码字符个数
    12    private $checkCode;    //验证码字符
    13    private $image;       //验证码画布
    14   /************************************************************************
    15   // Function:构造函数
    16   // Done:成员属性初始化
    17   // Author:www.5dkx.com 5D开心博客
    18 ************************************************************************/
    19   function __construct($width=60,$height=20,$codeNum=4)
    20   {
    21    $this->width = $width;
    22    $this->height = $height;
    23    $this->codeNum = $codeNum;
    24    $this->checkCode = $this->createCheckCode();
    25   }
    26   function showImage()
    27   {
    28    $this->getcreateImage();
    29    $this->outputText();
    30    $this->setDisturbColor();
    31    $this->outputImage();
    32   }
    33   function getCheckCode()
    34   {
    35    return $this->chekCode;
    36   }
    37   private function getCreateImage()
    38   {
    39    $this->image = imagecreatetruecolor($this->width,$this->height);
    40    $back = imagecolorallocate($this->image,255,255,255);
    41    $border = imagecolorallocate($this->image,255,255,255);
    42    imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
    43    //使用纯白色填充矩形框,这里用的话后面干扰码失效
    44    /*如果想用干扰码的话使用下面的*/
    45    //imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
    46   }
    47   private function createCheckCode()
    48   {
    49    for($i=0;$i<$this->codeNum;$i++)
    50    {
    51     $number = rand(0,2);
    52     switch($number)
    53     {
    54      case 0: $rand_number = rand(48,57); break;//数字
    55      case 1: $rand_number = rand(65,90);break;//大写字母
    56      case 2: $rand_number = rand(97,122);break;//小写字母
    57     }
    58     $asc = sprintf("%c",$rand_number);
    59     $asc_number = $asc_number.$asc;
    60    }
    61    return $asc_number;
    62   }
    63   private function setDisturbColor()//干扰吗设置
    64   {
    65    for($i=0;$i<=100;$i++)
    66    {
    67     //$color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
    68     $color = imagecolorallocate($this->image,255,255,255);
    69     imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
    70    }
    71     //$color = imagecolorallocate($this->image,0,0,0);
    72     //imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
    73    
    74   
    75   }
    76   private function outputText()
    77   {
    78    //随机颜色、随机摆放、随机字符串向图像输出
    79    for($i=0;$i<=$this->codeNum;$i++)
    80    {
    81     $bg_color = imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255));
    82     $x = floor($this->width/$this->codeNum)*$i+3;
    83     $y = rand(0,$this->height-15);
    84     imagechar($this->image,5,$x,$y,$this->checkCode[$i],$bg_color);
    85    }
    86   }
    87  
    88   private function outputImage()
    89   {
    90    if(imagetypes()&IMG_GIF)
    91    {
    92     header("Content_type:image/gif");
    93     imagegif($this->image);
    94    }
    95    elseif(imagetypes()&IMG_JPG)
    96    {
    97   
    98     header("Content-type:image/jpeg");
    99     imagejpeg($this->image,"",0.5);
    100    }
    101    elseif(imagetypes()&IMG_PNG)
    102    {
    103     header("Content-type:image/png");
    104     imagejpeg($this->image);
    105    }
    106    elseif(imagetypes()&IMG_WBMP)
    107    {
    108     header("Content-type:image/vnd.wap.wbmp");
    109     imagejpeg($this->image);
    110    }
    111    else
    112    {
    113     die("PHP不支持图像创建");
    114    }
    115   }
    116  
    117   function __destruct()
    118   {
    119    imagedestroy($this->image);
    120   }
    121 }
    122
    123 /*显示*/
    124 /*******************************************************************
    125 session_start();
    126 $image = new ImageCode(60,20,4);
    127 $image->showImage();
    128 $_SESSION['ImageCode'] = $image->getCheckCode();
    129 *******************************************************************/
    130
    131 ?>

  • 相关阅读:
    layui 获取select下拉选项上自定义的属性
    TP中关联模型的使用
    安卓模仿直播中的闪动(放大缩小)的动画
    Android报错Multiple dex files define Lcom/ta/utdid2/c/a/c
    Date.parse()的坑
    Android应用加固的简单实现方案(二)
    Android应用加固的简单实现方案
    Android中ANR的触发机制-BroadcastReceiver篇
    Android中ANR的触发机制-Service篇
    Application中以标准模式启动Activity报错的原因分析
  • 原文地址:https://www.cnblogs.com/softwaredevelop/p/1706611.html
Copyright © 2020-2023  润新知