ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP封装的验证码类

2021-07-06 11:02:00  阅读:146  来源: 互联网

标签:function rand arr 封装 img 验证码 mt PHP


<?php
/**
 * Created by PhpStorm.
 * Author:liuqun@
 * Date: 2018/7/25
 * Time: 0:54
 */
namespace Frame\Vendor;
//定义图片验证码类
final class Captcha
{
  //成员属性
    private  $code;        //验证码字符串
    private  $codelen;     //验证码长度
    private  $width;       //宽度
    private  $height;      //高度
    private  $img;         //图像资源句柄
    private  $fontfile;    //字体文件
    private  $fontsize;    //字体大小
    private  $fontcolor;   //字体颜色

    //构造方法
    public function __construct($codelen=4,$width=85,$height=40,$fontsize=20)
    {
        $this->codelen = $codelen; //验证码字符串长度
        $this->width   = $width;   //图像宽度
        $this->height  = $height;  //图像高度
        $this->fontfile= "./Public/Admin/Images/msyh.ttf";//字体文件
        $this->fontsize= $fontsize;
        $this->createCode();       //创建验证码字符串
        $this->createImg();        //创建图像资源
        $this->createBg();         //创建背景颜色
        $this->createFont();       //绘制文本
        $this->outPut();           //输出图像
    }
    //创建图像资源
    private  function  createImg()
    {
        $this->img = imagecreatetruecolor($this->width,$this->height);
    }

    //生成验证码
    private function createCode()
    {
        $str = "";
        $arr = array_merge(range("a","z"),range("A","Z"),range(0,9));
        shuffle($arr);
        shuffle($arr);
        $arr_index = array_rand($arr,$this->codelen);
        shuffle($arr_index);
        //生成随机字符串
        foreach($arr_index as $i)
        {
            $str .=$arr[$i];
        }
        //将字符串赋给$code属性
        $this->code = $str;
    }
    //生成背景
    private function createBg()
    {
        //随机背景色
        $color = imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
        //绘制矩形
        imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color);
    }
    //创建文字
    private function createFont()
    {
        $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
        imagettftext($this->img,$this->fontsize,0,5,30,$this->fontcolor,$this->fontfile,$this->code);
    }
    //输出
    private function outPut()
    {
        header("Content-type:image/png");
        imagepng($this->img);
        imagedestroy($this->img);
    }
    //获取验证码
    public function getCode()
    {
        return strtolower($this->code);
    }
}

调用的时候

  引进这个类,(注意引进命名空间)并且将从数据库中取出来的用户信息存储到$_SESSION 中
   //获取验证码方法
    public function captcha()
    {
        //创建验证码类的对象
        $captchaObj = new \Frame\Vendor\Captcha();
        //获取验证码字符串,并存入SESSION
        $_SESSION['captcha'] = $captchaObj->getCode();
    }

 

标签:function,rand,arr,封装,img,验证码,mt,PHP
来源: https://blog.51cto.com/u_15294355/2987243

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有