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

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

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

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

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

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

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

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

  9. 孟老板 ListAdapter封装, 告别Adapter代码 (四)

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

随机推荐

  1. HashSet和HashMap的区别

    HashSet和HashMap的区别.Java的HashSet类是由哈希表支持.它不保证 set 的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用 null 元素.HashSet类为基本操作提供 ...

  2. ubuntu-kylin16.04搭建lamp环境。

    首先下载安装apache2 输入:sudo apt-get install apache2 安装完毕后,在浏览器中输入:localhost 显示如下图,说明安装正确. 紧接着安装php7.0 输入:s ...

  3. IDE编辑器编码配置

    做跨平台开发时,大家用不同的IDE合作开发,最令人头疼的事就是各种乱码问题. 常用的IDE都支持utf-8编码和unix格式行尾'\n'. 1.XCODE设置文本编码及换行Xcode >> ...

  4. linux head、tail、sed、cut、grep、find

    head用法: head 参数 文件名 -cn:显示前n个字节    -n:显示前n行 例子:head -c20 1.txt 显示1.txt文件中前20个字符 ls |  head -20:显示前20 ...

  5. mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存

    增删改对二级缓存的影响 1.增删改也会清空二级缓存 2.对于二级缓存的清空实质上是对value清空为null,key依然存在,并非将Entry<k,v>删除 3.从DB中进行select查 ...

  6. kaggle数据挖掘竞赛初步--Titanic<派生属性&维归约>

    完整代码: https://github.com/cindycindyhi/kaggle-Titanic 特征工程系列: Titanic系列之原始数据分析和数据处理 Titanic系列之数据变换 Ti ...

  7. CGI,FastCGI,PHP-CGI与PHP-FPM

    CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...

  8. JQ返回顶部代码分享~~~~

    1.jq代码: <script type="text/javascript"> $(function() { $("#tbox").click(sc ...

  9. 一个在线jpg png转ICO的网站

    网站地址: https://lvwenhan.com/convertico/

  10. Linux vi

    修改linux服务器中的文件内容,使用vi编辑器 1.#vi [文件名] 2.点击i,进入编辑模式 3.要退出按ESC,进入中间模式,按冒号 :后面跟命令 :q!  (不保存并退出) :wq  (保存 ...