zoukankan      html  css  js  c++  java
  • PHP绘制验证码

    PHP绘制验证码(所有代码均为在网上找的,但我想自己熟悉一遍,所以自己写了一遍)

    一、在绘制之前,首先要了解PHP的图片处理库GD库:

      1、imagecreatetruecolor()

        创建一个真彩画布,该函数不支持gif格式。

         1 $img = imagecreatetruecolor(300,300); 

      2、绘制图像
        
          imagecolorallocate()
          
    imagecolorallocate($img,rand(0,255), rand(0,255),rand(0,255));
    //封装成哥随机函数
    function create_color($img)
    {
        return imagecolorallocate($img,rand(0,255), rand(0,255),rand(0,255));
    }

      3、向一个区域填充颜色。
            imagefill()
          1 imagefill($img,0,0,create_color($img)); 
      4、在画布中绘制一行文字
          imagestring() / imagechar()
           1 imagestring($img,4,20,150,'abcdefghijklmn',create_color($img)) 

    二、代码实现:

      1、制作二维码

        

     1 <?php
     2 /**
     3  * Created by PhpStorm.
     4  * User: monty
     5  * Date: 2018/10/24
     6  * Time: 10:18
     7  * function:实现验证码功能
     8  */
     9 ?>
    10 <?php
    11 
    12 //开启session
    13 session_start();
    14 //创建一个大小为 100*30 的验证码
    15 $image = imagecreatetruecolor(100, 30);
    16 $bgcolor = imagecolorallocate($image, 255, 255, 255);
    17 imagefill($image, 0, 0, $bgcolor);
    18 
    19 $captch_code = '';
    20 for ($i = 0; $i < 4; $i++) {
    21     $fontsize = 6;
    22     $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
    23     $data = 'abcdefghijkmnpqrstuvwxy3456789';
    24     $fontcontent = substr($data, rand(0, strlen($data) - 1), 1);
    25     $captch_code .= $fontcontent;
    26     $x = ($i * 100 / 4) + rand(5, 10);
    27     $y = rand(5, 10);
    28     imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
    29 }
    30 //就生成的验证码保存到session
    31 $_SESSION['authcode'] = $captch_code;
    32 
    33 //在图片上增加点干扰元素
    34 for ($i = 0; $i < 200; $i++) {
    35     $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
    36     imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor);
    37 }
    38 
    39 //在图片上增加线干扰元素
    40 for ($i = 0; $i < 3; $i++) {
    41     $linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));
    42     imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $linecolor);
    43 }
    44 //设置头
    45 header('content-type:image/png');
    46 imagepng($image);
    47 imagedestroy($image);
    48 
    49 ?>
    View Code

       2、验证:

        

     1 <?php
     2 /**
     3  * Created by PhpStorm.
     4  * User: monty
     5  * Date: 2018/10/24
     6  * Time: 10:20
     7  */
     8 ?>
     9 <?php
    10 
    11 if (isset($_REQUEST['authcode'])) {
    12     session_start();
    13     if (strtolower($_REQUEST['authcode']) == $_SESSION['authcode']) {
    14         echo "输入正确!";
    15     } else {
    16         echo "输入错误!";
    17     }
    18     exit();
    19 }
    20 ?>
    21 
    22 <!DOCTYPE html>
    23 <html>
    24 <head>
    25     <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    26     <title>确认验证码</title>
    27 </head>
    28 <body>
    29 <form method="post" action="./form.php">
    30     <p>验证码图片:
    31         <img id="captcha_img" border="1" src="./captcha.php?r=<?php echo rand(); ?>" width=100 height=30>
    32         <a href="javascript:void(0)"
    33            onClick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">换一个</a>
    34     </p>
    35     <p>请输入图片中的内容:<input type="text" name="authcode" value=""/></p>
    36     <p><input type="submit" value="提交" style="padding:6px 20px;"></p>
    37 </form>
    38 </body>
    39 </html>
    View Code
     












     
  • 相关阅读:
    Beta冲刺<7/10>
    Beta冲刺<6/10>
    Beta冲刺<5/10>
    Beta冲刺--冲刺总结
    Beta冲刺<4/10>
    实验四
    结对编程第二阶段
    实验二 结对编程第一阶段
    实验报告
    团队作业第六次——Beta冲刺
  • 原文地址:https://www.cnblogs.com/yangsongwei/p/9842212.html
Copyright © 2011-2022 走看看