封装captcha类 -- 画图四

时间:2021-12-13 02:48:32
<?php

    // 验证码类
class Captcha{
//属性
private $width;
private $height;
private $length;
private $lines;
private $pixels;
private $color;
private $font;
private $string;
/*
*构造方法
*@param1 array $arr, 一个数组, 里面几乎包含了所有的属性
*
*/
public function __construct($arr = array()) {
$this->width = isset($arr['width']) ? $arr['width'] : 146;
$this->height = isset($arr['height']) ? $arr['height'] : 20;
$this->length = isset($arr['length']) ? $arr['length'] : 4;
$this->lines = isset($arr['lines']) ? $arr['lines'] : 5;
$this->pixels = isset($arr['pixels']) ? $arr['pixels'] : 200;
$this->font = isset($arr['font']) ? $arr['font'] : 5;
// 背景色
$this->color['bg_min'] = isset($arr['bg_min']) ? $arr['bg_min'] : 200;
$this->color['bg_max'] = isset($arr['bg_max']) ? $arr['bg_max'] : 255;
// 字体颜色
$this->color['font_min'] = isset($arr['font_min']) ? $arr['font_min'] : 0;
$this->color['font_max'] = isset($arr['font_max']) ? $arr['font_max'] : 100;
// 线颜色
$this->color['line_min'] = isset($arr['line_min']) ? $arr['line_min'] : 100;
$this->color['line_max'] = isset($arr['line_max']) ? $arr['line_max'] : 150;
// 像素颜色
$this->color['pixels_min'] = isset($arr['pixels_min']) ? $arr['pixels_min'] : 150;
$this->color['pixels_max'] = isset($arr['pixels_max']) ? $arr['pixels_max'] : 200;
// 字符串
$this->string = isset($arr['string']) ? $arr['string'] : 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789'; } /*
*获得验证码图片
*
*/
public function generate() { //1. 创建画布
$im = imagecreatetruecolor($this->width, $this->height); //2. 背景顏色
//2.1分配顏色
$bg_color = imagecolorallocate($im, mt_rand($this->color['bg_min'], $this->color['bg_max']), mt_rand($this->color['bg_min'], $this->color['bg_max']), mt_rand($this->color['bg_min'], $this->color['bg_max'])); //2.2背景填充
imagefill($im, 0, 0, $bg_color); // 3.获取验证码
$captcha = $this->getCaptchaStr(); // var_dump($captcha); exit; // 4.分配颜色
$str_color = imagecolorallocate($im, mt_rand($this->color['font_min'], $this->color['font_max']), mt_rand($this->color['font_min'], $this->color['font_max']), mt_rand($this->color['font_min'], $this->color['font_max'])); //5. 将验证码写入到图片
imagestring($im, $this->font, ceil($this->width / 2) - 20, ceil($this->height / 2) - 10, $captcha, $str_color); // 6. 增加干扰线
for( $i = 0; $i < $this->lines; $i++ ){
// 分配颜色
$line_color = imagecolorallocate($im, mt_rand($this->color['line_min'], $this->color['line_max']), mt_rand($this->color['line_min'], $this->color['line_max']), mt_rand($this->color['line_min'], $this->color['line_max']));
// 写入线段
imageline($im, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $line_color);
} // 7. 增加干扰点
for( $i = 0; $i < $this->pixels; $i++ ){
$pixels_color = imagecolorallocate($im, mt_rand($this->color['pixels_min'], $this->color['pixels_max']), mt_rand($this->color['pixels_min'], $this->color['pixels_max']), mt_rand($this->color['pixels_min'], $this->color['pixels_max']));
// 写入线段
imagesetpixel($im, mt_rand(0, $this->width), mt_rand(0, $this->height), $pixels_color); } // 8. 保存输出
imagepng($im);
// imagepng($im, 'captcha.png'); // 9. 释放资源
imagedestroy($im); } /*
*获得验证码字符串
*
*/
private function getCaptchaStr() {
// 定義變量保存字符串
$captchaStr = '';
// for( $i = 0; $i < $this.length; $i++ ){ //傻逼写法
for( $i = 0; $i < $this->length; $i++ ){
// 获取随机字符串
$captchaStr .= $this->string[mt_rand(0, strlen($this->string) - 1)];
} // 将随机字符串存放在session中
$_SESSION['captcha'] = $captchaStr; return $captchaStr;
} } $captcha = new Captcha(); // header('content-type: image/php');
header('content-type: image/png');
$captcha->generate();

封装captcha类 -- 画图四的更多相关文章

  1. 领域模型中的实体类分为四种类型:VO、DTO、DO、PO

    http://kb.cnblogs.com/page/522348/ 由于不同的项目和开发人员有不同的命名习惯,这里我首先对上述的概念进行一个简单描述,名字只是个标识,我们重点关注其概念: 概念: V ...

  2. 封装application类

    <?php  //判断用户是否是通过入口文件访问   if(!defined('ACCESS')){     echo '非法请求';     die;   }   //封装初始化类   cla ...

  3. PHP连接数据库:封装成类

    php连接数据库,操作他增删改查等操作,其中要多次连接数据库,每个页面也需要连接数据库,更改数据会及其麻烦: 为了便于数据库的更改,我们可以把固定的那几句话封装成类,这样虽然代码量也差不多,但是有利于 ...

  4. 域模型中的实体类分为四种类型:VO、DTO、DO、PO

    经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度,对这几个概念进行简析. 得出的主要结论是:在项目应用中,VO对应于页面上需要显示的数据(表单),DO对应 ...

  5. c&num;封装DBHelper类 c&num; 图片加水印 &lpar;摘&rpar;C&num;生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c&num; 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)

    c#封装DBHelper类   public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...

  6. php使用GD库实现图片水印和缩略图——封装成类

    学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来 ...

  7. 适用于app&period;config与web&period;config的ConfigUtil读写工具类 基于MongoDb官方C&num;驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP&period;NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C&num; 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  8. 转&colon;领域模型中的实体类分为四种类型:VO、DTO、DO、PO

    经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度,对这几个概念进行简析.得出的主要结论是:在项目应用中,VO对应于页面上需要显示的数据(表单),DO对应于 ...

  9. 孟老板 ListAdapter封装&comma; 告别Adapter代码 &lpar;四&rpar;

    BaseAdapter系列 ListAdapter封装, 告别Adapter代码 (一) ListAdapter封装, 告别Adapter代码 (二) ListAdapter封装, 告别Adapter ...

随机推荐

  1. 移动端全屏滑动的小插件,简单,轻便,好用,只有3k swiper&comma;myswiper&comma;page&comma;stage

    https://github.com/donglegend/mySwiper mySwiper 移动端全屏滑动的小插件,简单,轻便,好用,只有3k 下载 直接下载 bower install mySw ...

  2. Divide and Conquer&colon;Monthly Expense&lpar;POJ 3273&rpar;

    Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...

  3. 钉子和小球&lowbar;DP

    Description 有一个三角形木板,竖直立放,上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周围的钉子的距离都等于d,每个格子的宽度也都等于d,且除了最左端 ...

  4. glance image cache

    The Glance Image Cache The Glance API server may be configured to have an optional local image cache ...

  5. SpringBoot几种定时任务的实现方式

    定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...

  6. h5适配的解决方案

    一. 流程 设计师以750pt×1334pt尺寸进行设计(当然高度随内容变化),最后用该尺寸的设计稿进行标注.切图,前端采用淘宝的开源方案flexible进行适配. 二. flexible使用方法 F ...

  7. BZOJ1023 SHOI2008 仙人掌图 仙人掌、单调队列

    传送门 求仙人掌的直径,可以由求树的直径进行拓展,只需要在环上特殊判断. 沿用求树的直径的DP,对于一条不在任何环内的边,直接像树的直径一样转移,然后考虑环的影响. 设环长为\(cir\),在\(df ...

  8. &lpar;转)为什么wait&lpar;&rpar;,notify&lpar;&rpar;和notifyAll&lpar;&rpar;必须在同步块或同步方法中调用

    我们常用wait(),notify()和notifyAll()方法来进行线程间通信.线程检查一个条件后就行进入等待状态,例如,在“生产者-消费者”模型中,生产者线程发现缓冲区满了就等待,消费者线程通过 ...

  9. 1-VScode格式化ESlint-方法(最全最好用方法!)

    1-VScode格式化ESlint-方法(最全最好用方法!)   ESlint:是用来统一JavaScript代码风格的工具,不包含css.html等. 背景: 近来研究前端,然后一直在百度上找VSc ...

  10. 那种多空计算方法更正确呢?——从此图看应该是TEST005

    那种方法计算多空逆转更正确呢?——从此图1看应该是TEST005,但是实际上是ZCL_多空! TEST005具有滞后性!也就是说跌了一些在报警,可能已经跌了10%(如图2) ZCL_多空:当计算结果和 ...