php生成验证码

<form action="2.php" method="post">
        <input type="text" name="code">
        <img src="Control.php" alt="验证码" onclick="this.src = 'Control.php?'+Math.random()">
        <button>提交</button>
    </form>
<?php
//2.php
session_start();
if(strtolower($_POST['code']) === $_SESSION['code']) {
    echo 'ok';
} else {
    echo 'error';
}
?>
<?php
//Control.php
session_start();
include 'Captcha.php';
// //可以传递三个参数 分别是 长度 高度 验证码字符长度
$captcha = new Captcha(200,80,5);
$code = $captcha->render();
$_SESSION['code'] = $code;
// file_put_contents('code.php',$code);
?>
//Captcha.php
<?php
  class Captcha {
      protected $width; //画布宽度
      protected $height; //画布高度
      protected $res; //画布
      protected $len;//验证码长度
      protected $code; //验证码
      public function __construct(int $width = 100,int $height = 30,int $len = 4) {
          $this->width = $width;
          $this->height = $height;
          $this->len = $len;
      }
      public function render() {
          $res = imagecreatetruecolor($this->width,$this->height);
          imagefill($this->res=$res,0,0,imagecolorallocate($res,200,200,200));
          $this->text();
          $this->line();
          $this->pix();
          $this->show();
          return $this->code;
      }
      protected function show() {
        header('Content-type:image/png');
        imagepng($this->res);
      }
      //生成随机码
      protected function text() {
          $font = realpath('SourceHanSansSC-Normal.otf');
          $text = 'abcdefghijklmnopqrstuvwxyz123456789';
          for($i=0;$i<$this->len;$i++) {
              $x = $this->width / $this->len;
              $angle = mt_rand(-20,20);
              $box = imagettfbbox(20,$angle,$font,'A');
              $this->code .= $code = strtoupper($text[mt_rand(0,strlen($text)-1)]);
              imagettftext(
                  $this->res,
                  20,
                  mt_rand(-20,20),
                  $x*$i +10,
                  $this->height / 2 - ($box[7] - $box[0]) /2,
                  $this->textColor(),
                  $font,
                  $code
              );
          };
      }
      //生成随机颜色的干扰线
      protected function line() {
        for($i=0;$i<6;$i++) {
            imageline(
                $this->res,
                mt_rand(0,$this->width),
                mt_rand(0,$this->height),
                mt_rand(0,$this->width),
                mt_rand(0,$this->height),
                $this->color()
            );
        }
      }
      //生成随机颜色
      protected function color() {
          return imagecolorallocate($this->res,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
      }
      protected function textColor() {
        return imagecolorallocate($this->res,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
      }
      //绘制干扰点
      protected function pix() {
          for($i=0;$i<300;$i++) {
              imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->color());
          }
      }
  }
?>
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值