thinkphp验证码

<?php
namespace captcha;

class Captcha {

  private $image;
  private $width = 80;
  private $height = 35;

  public function create_captcha($code) {
    //创建彩色画布: param1: 图片宽度, param2: 图片高度
    $this->image = imagecreatetruecolor($this->width, $this->height);
    //为一幅图像分配颜色, param1: 画布资源, param2: red; param3: green, param4: blue
    $bgcolor = imagecolorallocate($this->image, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
    $textColor = imagecolorallocate($this->image, 0, 0, 0);
    //填充矩形颜色, param1: 画布资源, param2: 填充x起点, param3: 填充y起点,param4: 填充x终点, param5: 填充y终点, param6: 要填充的颜色,由imagecolorallocate创建的颜色资源
    imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $bgcolor);
    $this->create_interference();
    $font = dirname(__FILE__) . "/atnadiannemedium.ttf";
    //水平绘制字符串, param1: 画布资源, param2: 字符串尺寸,param3: 绘制角度, param4:左上角的x坐标, param5:左上角的y坐标, param6: 颜色资源,param7: 字体文件, param8:字符串内容 
    imagettftext($this->image, 20, mt_rand(-10, 10), 8, 25, $textColor, $font, $code);
    //设置头部,image/png
    header('Content-type: image/png');
    //向浏览器或文件输出PNG图像, param1: 画布资源
    imagepng($this->image);
    // 销毁画布
    imagedestroy($this->image);
  }

  public function rand_code() {
    $str = "abcdefghijklmnopqrstuvwxyz0123456789";
    $code = '';
    for($i = 0; $i < 4; $i++) {
      $code .= $str[mt_rand(0, strlen($str) - 1)];
    }
    return $code;
  }

  private function create_interference() {
    //线条
    for ($i = 0; $i < 6; $i++) {
     $color = imagecolorallocate($this->image, mt_rand(10, 100), mt_rand(10, 100), mt_rand(10, 100));
     // 画一条线条,param1: 画布资源,param2: 填充x起点, param3: 填充y起点,param4: 填充x终点, param5: 填充y终点,param6: 颜色资源
     imageline($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
    }
    //干扰
    for ($i = 0; $i < 100; $i++) {
     $color = imagecolorallocate($this->image, mt_rand(100, 200), mt_rand(100, 200), mt_rand(100, 200));
     // 水平地画一行字符串, param1: 画布资源,param2: 颜色尺寸(1-5), param3: 填充x起点, param4: 填充y起点, param5: 字符串内容, param6: 颜色资源
     imagestring($this->image, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), 'X', $color);
    }
   }
}
?>

目录: extend/captcha/Captcha.php

使用

<?php
namespace app\backend\controller;

use think\Controller;
use think\Request;
use think\Session;
use captcha\Captcha as CaptchaClass;

class Captcha extends Controller
{
  public function get_captcha() {
    $captcha = new CaptchaClass();
    $code = $captcha->rand_code();
    Session::set('captcha', $code);
    $captcha->create_captcha($code);
  }
}

?>

 

<input class="login-code" type="text" name="code" placeholder="验证码" value="" /><img src="/backend/get_captcha" width="60" height="30" onclick="this.src='/backend/get_captcha?'+Math.random();" />

用到的主要函数

resource imagecreatetruecolor ( int $width , int $height )

int imagecolorallocate ( resource $image , int $red , int $green , int $blue )

bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile, string $text )

bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资源 5来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资 5源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值