<?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. Java之工厂方法

    普通工厂模式: 第一步:定义接口,坚持面向接口编程, package dp; public interface Sender {    public void send();} 第二步:实现接口: p ...

  2. PackageManager源码分析

    在android 4.4源码上进行的分析. 一.PackageManager如何产生的? 我们平时在代码中使用的context.getPackageManager() 那么这个PackageManag ...

  3. gulp入门教程

    第1步:安装Node 首先,最基本也最重要的是,我们需要搭建node环境.访问 nodejs.org,下载完成后直接运行程序,就一切准备就绪.npm会随着安装包一起安装,稍后会用到它. 为了确保Nod ...

  4. Mediawiki

    摘要: 本文讲述如何使用Mediawiki搭建个人wiki系统.以及wiki的部分基本操作和设置 日常开发中总会有许多东西需要记录,很多软件都可以做到这一点,例如有道云笔记,或者写博客.但个人偏爱于M ...

  5. 三、最小化的Spring XML配置

    Spring 提供了自动装配(自动识别如何装配Bean的依赖关系)和自动检测(检测哪些类需要被配置成Spring Bean) 1.自动装配Bean的属性 1.1四种类型得自动装配:byName.byT ...

  6. C# WebForm内置对象2+Repeater的Command

    内置对象:用于页面之间的数据交互 为什么要使用这么内置对象?因为HTTP的无状态性. Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及 ...

  7. sass2:

    ass学习笔记2 今天介绍sass在重用代码时最具威力的两个功能.一个是嵌套(Nesting),一个混合(Mixin). 我们在写CSS通过需要多个后代选择器组合到一起才能定位到目标元素上,而这定义过 ...

  8. iOS开发:自定义控件实现手势解锁

    自定义控件 1.提供initWithFrame:及initWithCoder:方法来初始化: 2.解锁控件只负责展示.触摸.绘图等,存储轨迹.判断轨迹等操作不是解锁控件要做的.因此要定义一个代理,将轨 ...

  9. 企业项目开发--分布式缓存Redis

    第九章 企业项目开发--分布式缓存Redis(1) 注意:本章代码将会建立在上一章的代码基础上,上一章链接<第八章 企业项目开发--分布式缓存memcached> 1.为什么用Redis ...

  10. 把word文档中的所有图片导出

    把word文档中的所有图片导出 end