• PHP学习笔记09——GD生成验证码实例


    vcode.class.php

     1 <?php
     2     class Vcode{
     3         private $width;
     4         private $height;
     5         private $codeNum;
     6         private $disturbColorNum;    //干扰元素数量
     7         private $checkCode;
     8         private $image;
     9 
    10         //构造方法,参数初始化,并生成验证码
    11         function __construct($width = 80, $height = 20, $codeNum = 4) {
    12             $this->width = $width;
    13             $this->height = $height;
    14             $this->codeNum = $codeNum;
    15             $number = floor($height * $width / 15);
    16             //计算干扰元素数量
    17             if ($number > 240 - $codeNum) 
    18                 $this->disturbColorNum = 240 - $codeNum;
    19             else 
    20                 $this->disturbColorNum = $number;
    21             //创建验证码
    22             $this->checkCode = $this->createCheckCode();
    23         }
    24         //析构方法,销毁图片资源
    25         function __destruct(){
    26             imagedestroy($this->image);
    27         }
    28         //随机生成验证码
    29         private function createCheckCode(){
    30             $code = "3456789abcdeffghijkmnpqrstuvwxyzABCDEFHIJKMNPQRSTUVWXYZ";
    31             $ascii="";
    32             for ($i = 0; $i < $this->codeNum; $i++) {
    33                 $ascii .= $code{rand(0,strlen($code)-1)};
    34             }
    35             return $ascii;
    36         }
    37         //用于输出验证码图片
    38         function __toString(){
    39             $_SESSION["code"] = strtoupper($this->checkCode);
    40             $this->outimg();
    41             return "";
    42         }
    43         //输出图片的具体步骤
    44         private function outimg(){
    45             $this->getCreateImage();
    46             $this->setDisturbcolor();
    47             $this->outputText();
    48             $this->outputImage();
    49         }
    50         //初始化图片
    51         private function getCreateImage(){
    52             $this->image = imagecreatetruecolor($this->width, $this->height);
    53             $backColor = imagecolorallocate($this->image, rand(225,255), rand(225,255), rand(225,255));
    54             imagefill($this->image, 0, 0, $backColor);
    55             $borderColor = imagecolorallocate($this->image, 0, 0, 0);
    56             imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $borderColor);
    57         }
    58         //输出不同颜色的点作为干扰素,点加弧线
    59         private function setDisturbcolor(){
    60             for ($i = 0; $i <= $this->disturbColorNum; $i++) {
    61                 $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
    62                 imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
    63             }
    64             for ($i = 0; $i <10; $i++) {
    65                 $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
    66                 imagearc($this->image, rand(-10,$this->width), rand(-10,$this->height), rand(30,300), 
    67                     rand(20,200), 55, 44, $color);
    68             }    
    69         }
    70         //随机在图片中生成字符串
    71         private function outputText(){
    72             for ($i = 0; $i < $this->codeNum; $i++) {
    73                 $fontcolor = imagecolorallocate($this->image, rand(0,128), rand(0,128), rand(0,128));
    74                 $fontsize = rand(4, 5);
    75                 $x = floor($this->width/$this->codeNum)*$i + 3;
    76                 $y = rand(0, $this->height-imagefontheight($fontsize));
    77                 imagechar($this->image, $fontsize, $x, $y, $this->checkCode{$i}, $fontcolor);
    78             }
    79         }
    80         //输出图片
    81         private function outputImage(){
    82             header("Content-type: image/png");
    83             imagepng($this->image);
    84         }
    85         
    86     }
    87 ?>

    imgcode.php

    1 <?php
    2     session_start();                    //开启session
    3     require_once ('vcode.class.php');        //包含验证码文件
    4     echo new Vcode();
    5 ?>

    image.php

     1 <?php
     2     session_start();    //开启session
     3     
     4     if (isset($_POST['submit'])) {
     5         if (strtoupper($_POST['code']) == $_SESSION['code']) 
     6             echo "验证码输入正确<br/>";
     7         else 
     8             echo '<font color="red">验证码输入错误<font/><br/>';
     9     }
    10 ?>
    11 <html>
    12     <head>
    13         <script type="text/javascript">
    14             function newgdcode(obj, url) {
    15                 //加一个随机参数以免浏览器不刷新
    16                 obj.src = url+'?nowtime='+new Date().getTime();
    17             }
    18         </script>
    19     </head>
    20     <body>
    21         <img alt="看不清楚,换一张" src="imgcode.php" style="cursor: pointer;" 
    22                         onclick="javascript:newgdcode(this, this.src);" />
    23         <form action="image.php" method="post">
    24             <input type="text" size="4" name="code" />
    25             <input type="submit" name="submit" value="提交" />
    26         </form>
    27     </body>
    28 </html>

    执行结果

  • 相关阅读:
    8102Java的学习呼声有所下降,2019年还值得学习吗
    大数据开发面试题详解:Hadoop的运行原理
    总结:java大神的自学路线
    大数据开发工程师必看书籍
    java主要编程工具
    bzoj3791 作业
    bzoj3750 [POI2015]Pieczęć
    bzoj1143 [CTSC2008]祭祀river
    bzoj2718 [Violet 4]毕业旅行
    poj3237 Tree
  • 原文地址:https://www.cnblogs.com/swm8023/p/3261869.html
Copyright © 2020-2023  润新知