一、效果图

二、类代码

<?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

最新文章

  1. Zabbix监控mysql performance
  2. 基于AgileEAS.NET SOA 中间件领域模型数据器快速打造自己的代码生成器
  3. jQuery 点击显示再次点击隐藏
  4. Win7(32bit)下Qt5.5.0和OpenCV2.4.9环境的搭建
  5. 自然语言16.1_Python自然语言处理学习笔记之信息提取步骤&amp;分块(chunking)
  6. 攻城狮在路上(壹) Hibernate(十四)--- Hibernate的检索方式(下)
  7. Windows Server 2012下安装Hyper-V虚拟机
  8. HDU-4628 Pieces 搜索 | DP
  9. qemu 调试(二)
  10. Android中的一些基础知识(二)
  11. paip.jdk1.4 1.5(5.0) 1.6(6.0) 7.0 8.0特点比较与不同
  12. c#的as关键字
  13. c++字符串的输入的思考
  14. Milk Patterns poj3261(后缀数组)
  15. 基于zookeeper的Swarm集群搭建
  16. Linux 系统下在线安装 Tomcat
  17. zookeeper的三种安装模式
  18. vue cli3.0 结合echarts3.0和地图的使用方法
  19. html 刷新重载方法汇总
  20. 2080 特殊的质数肋骨 USACO (深度优先搜索)

热门文章

  1. C#特性 详解
  2. 博客自定义1-皮肤模板 基于SimpleMemory 添加到顶部小按钮
  3. 【小知识点】解决Chrome动画”卡顿”的办法
  4. iOS热更新实现方式
  5. (转载)关于FLASH寿命的读写方法
  6. Linux中找回误删除的文件
  7. sleep() 和 wait() 有什么区别?(未完成)
  8. 《JavaScript DOM编程艺术》(第二版)学习笔记(一)
  9. WCF Windows基础通信
  10. flask 杂记