一、效果图

二、类代码

<?php
/**
* Created by PhpStorm.
* User: Yang
* Date: 2019/8/13
* Time: 10:51
*/ class Captcha
{
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
private $code;//验证码
private $codelen = 4;//验证码长度
private $width = 130;//宽度
private $height = 50;//高度
private $img;//图形资源句柄
private $font;//指定的字体
private $fontsize = 20;//指定字体大小
private $fontcolor;//指定字体颜色 //构造方法初始化
public function __construct()
{
$this->font = dirname(__FILE__) . '/font/Elephant.ttf';//注意字体路径要写对,否则显示不了图片
} //生成随机码
private function createCode()
{
$_len = strlen($this->charset) - 1;
for ($i = 0; $i < $this->codelen; $i++) {
$this->code .= $this->charset[mt_rand(0, $_len)];
}
} //生成背景
private function createBg()
{
//创建真彩图像资源
$this->img = imagecreatetruecolor($this->width, $this->height);
//分配一个随机色
$color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
//画一矩形并填充随机色
imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
} //生成文字
private function createFont()
{
$_x = $this->width / $this->codelen;
for ($i = 0; $i < $this->codelen; $i++) {
$this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
//可以绘制汉字
//绘制验证码
imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
}
} //生成线条、雪花
private function createLine()
{
//线条
for ($i = 0; $i < 6; $i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imageline($this->img, 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->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
}
} //输出
private function outPut()
{
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
} //对外生成
public function createImage()
{
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
} //获取验证码
public function getCode()
{
return strtolower($this->code);
}
}

三、使用

<?php
/**
* Created by PhpStorm.
* User: Yang
* Date: 2019/8/13
* Time: 11:02
*/ include "./Captcha.php"; $c = new Captcha();
$c->createImage();

四、源代码

链接:https://pan.baidu.com/s/1i2MNN-y0xl4Qsfhn0EhGJQ
提取码:qtfx

PHP 之验证码类封装的更多相关文章

  1. THINKPHP源码学习--------验证码类

    TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...

  2. 【个人使用.Net类库】(4)验证码类

    验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...

  3. PHP验证码类

    通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现的细节封装在该类中.只要在创建对象时,为构造方法提供三个参数 ...

  4. 一个经典的PHP验证码类分享

    我们通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现 的细节封装在该类中.只要在创建对象时,为构造方法提供三 ...

  5. php面向对象(OOP)---- 验证码类

    PHP常用自封装类--验证码类 验证码是众多网站登陆.注册等相关功能不可以或缺的功能,实现展示验证码的方式有很多,这篇文章作者以工作中比较常用的方法进行了封装. 逻辑准备 要实现一个完整的验证码,需要 ...

  6. PHP面向对象简易验证码类

    PHP简易验证码类 <?php class authCode { private static $instance = null; #实例对象 private $width = 120; #图片 ...

  7. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  8. salesforce 零基础学习(四十八)自定义列表分页之Pagination基类封装 ※※※

    我们知道,salesforce中系统标准列表页面提供了相应的分页功能,如果要使用其分页功能,可以访问http://www.cnblogs.com/zero-zyq/p/5343287.html查看相关 ...

  9. ThinkPHP 3.2.3 加减乘法验证码类

    ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Ver ...

随机推荐

  1. 改善C#程序的方法

    写在开头: http://www.cnblogs.com/luminji    157个建议_勘误表 一:属性 属性和方法一样.也可以是virtual和abstract. 条款2:运行时常量(read ...

  2. eventFlow 系列 <三> 查询所有

    接着上面的例子,产生2条数据.怎么把这两条数据查询出来呢? var commandBus = resolver.Resolve<ICommandBus>(); , ); var execu ...

  3. linux服务脚本

    #!/bin/sh ARG=$1 case $ARG in start): nohup /path/program & ;; stop): pkill program ;; restart): ...

  4. MyBatis工厂工具类 MyBatisUtils

    import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apach ...

  5. rabbit MQ 的环境及命令使用(一)

    RabbitMQ依赖erlang,所以先安装erlang,然后再安装RabbitMQ; 先安装erlang,双击erlang的安装文件即可,然后配置环境变量: ERLANG_HOME=D:\Progr ...

  6. C 预编译 宏 声明

  7. HDU 6568 Math

    Math \(f_i\)为从\(i\)到\(i+1\)的期望步数. \(f_i = 1-p + p(f_i + 2((1-q)^{n-i}(n-i) + q\sum_{j=0}^{n-i-1}(1-q ...

  8. Linux文件系统之install(复制文件和设置文件属性)

    install命令 install命令的作用是安装或升级软件或备份数据,它的使用权限是所有用户.install命令和cp命令类似,都可以将文件/目录拷贝到指定的地点.但是,install允许你控制目标 ...

  9. php文件更新后不生效?亲测有效!

    1,问题描述 一台windows Server2008 服务器上运行了iis7,其中存在php5.3和php5.5引擎的网页服务. 但实际使用中发现,修改php文件后,访问该文件的结果经常不能实时刷新 ...

  10. 一个ball例程带你进入 Halcon 世界

    * 此例程来自halcon自带例程,请打开 halcon->ctrl+E 打开例程->搜索框中输入ball added by xiejl* ball.hdev: Inspection of ...