如何在PHP中定义一个验证码工具类

发布时间:2021-05-12 16:39:39 作者:Leah
来源:亿速云 阅读:129

今天就跟大家聊聊有关如何在PHP中定义一个验证码工具类,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

//验证码宽度
private $width;
//验证码高度
private $height;
//验证的个数
private $length;
//干扰点个数
private $dots;
//干扰点的类型
private $type;
//干扰线个数
private $lines;
//文字
private $font;

方便在项目中对验证码的要求进行更改,以方便项目逻辑的需求,而且验证码所选用的字体需要和验证码工具类放在同一目录下,否则就要在配置文件中修改字体的路径才能实现验证码的显示

<?php
//创建验证码工具类
class captcha {
 //验证码的各种参数
 //验证码宽度
 private $width;
 //验证码高度
 private $height;
 //验证的个数
 private $length;
 //干扰点个数
 private $dots;
 //干扰点的类型
 private $type;
 //干扰线个数
 private $lines;
 //文字
 private $font;
 //验证码属性的构造方法
 public function __construct($arr = array ()) {
  //将属性赋值
  $this->width = isset ($arr['width']) ? trim($arr['width']) : '270';
  $this->height = isset ($arr['height']) ? trim($arr['height']) : '30';
  $this->length = isset ($arr['length']) ? trim($arr['length']) : '4';
  $this->dots = isset ($arr['dots']) ? trim($arr['dots']) : '81';
  $this->type = isset ($arr['type']) ? trim($arr['type']) : '*';
  $this->lines = isset ($arr['lines']) ? trim($arr['lines']) : '5';
  $this->font = isset ($arr['font']) ? trim($arr['font']) : './cambriab.ttf';
 }
 //创建验证码的方法
 public function captcha() {
  //创建画布
  $img = imagecreatetruecolor($this->width, $this->height);
  //填充颜色
  //颜色资源
  $color = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  //填充背景
  imagefill($img, 0, 0, $color);
  //添加干扰点
  for ($i = 0; $i < $this->dots; $i++) {
   //颜色资源
   $dots_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
   //插入干扰点
   imagestring($img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), $this->type, $dots_color);
  }
  //添加干扰线
  for ($i = 0; $i < $this->lines; $i++) {
   //颜色资源
   $line_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
   //插入干扰线
   imageline($img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $line_color);
  }
  //首先获取验证码,然后插入验证文字
  //文字高度
  $size = mt_rand(18, 20);
  //获取验证码
  $str = $this->captchastring();
  for ($i = 0; $i < strlen($str); $i++) {
   //颜色资源
   $str_color = imagecolorallocate($img, mt_rand(50, 150), mt_rand(50, 150), mt_rand(50, 150));
   //插入验证码
   imagettftext($img, $size, mt_rand(-45, 45), $this->width / ($this->length + 2) * ($i +1), (($this->height - $size) / 2) + $size, $str_color, $this->font, $str[$i]);
  }
  //将得到的验证码存入SESSION中,便于以后的验证码判断
  @ session_start();
  $_SESSION['captcha'] = $str;
  //输出图片
  header("content-type:image/png");
  imagepng($img);
  //清除资源
  imagedestroy($img);
 }
 //获取随机的验证内容:A-Z,a-z,1-9
 private function captchaString() {
  //获取四个随机的字符串
  $str = "";
  for ($i = 0; $i < $this->length; $i++) {
   switch (mt_rand(1, 3)) {
    case 1 :
     $str .= chr(mt_rand(49, 57));
     break;
    case 2 :
     $str .= chr(mt_rand(97, 122));
     break;
    case 3 :
     $str .= chr(mt_rand(65, 90));
     break;
   }
  }
  return $str;
 }
 //判断验证码
 public static function checkCaptcha($captcha) {
  @ session_start();
  return strtoupper($captcha) === strtoupper($_SESSION['captcha']);
 }
}
//使用方法:
$img = new captcha();//这里采用默认参数
$img->captcha();
?>

运行结果:

如何在PHP中定义一个验证码工具类

PHP开发环境搭建工具有哪些

一、phpStudy,是一个新手入门最常用的开发环境。二、WampServer,WampServer也同样的也是和phpStudy一样操作简单对小白比较友好。三、XAMPP,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包;四、MAMP,MAMP分为两种MAMP和MAMP Pro for Mac。五、宝塔面板,宝塔面板是一款服务器管理软件,支持windows和linux系统。六、UPUPW,UPUPW是目前Windows平台下最具特色的Web服务器PHP套件。

看完上述内容,你们对如何在PHP中定义一个验证码工具类有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. Jackson的JSON工具类如封装 JsonUtils用法
  2. 如何在PHP中定义组合模式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:如何在PHP中定义模板模式

下一篇:如何在CSS中使用 var()变量

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》