<?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. 解决首次访问jenkins,输入默认密码之后,一直卡住问题

    简介   安装系统:centos6.5 安装方式:在官网中下载jenkins.war,放到tomcat根目录下的webapps中,启动tomcat(还有一种yum在线安装的方式) 1.首次访问,出现如 ...

  2. CIDR风格

    CIDR地址中包含标准的32位IP地址和有关网络前缀位数的信息. eg: 地址:192.168.10.0/24,其中/24标示前面地址中的前24位代表网络部分,其余代表主机部分. 11000000 1 ...

  3. IIS 常见问题集记录

    win7 iis7.5 详细错误信息模块 IIS Web Core 通知 BeginRequest 处理程序 尚未确定 错误代码 0x80070021 配置错误 不能在此路径中使用此配置节.如果在父级 ...

  4. CentOS7 yum 安装git

    1.查看系统是否已经安装git git --version 2.CentOS7 yum 安装git yum install -y git 3.安装成功 4.卸载git yum remove git

  5. asp.net js获取控件ID

    ClientID是由ASP.Net生成的服务器控件得客户端标识符,当这个控件生成到客户端页面的时候,在客户端代码访问该控件时就需要通过ClientID来访问. 以文本框为例: 一.未使用母版页 js可 ...

  6. 性能adb命令

    启动时间-冷启动启动App命令adb shell am start -W -n com.bit_health.android/.ui.common.activities.BitHealthMainAc ...

  7. react参考项目001

    https://github.com/chen2009277025/webpack-ant-design-demo https://github.com/cobish/cobish.github.io ...

  8. 学习docker

    虚拟机下Ubuntu环境 1.sudo apt-get update 2.sudo apt-get install docker.io 3.在daocloud(http://www.daocloud. ...

  9. Nand Flash,Nor Flash,CFI Flash,SPI Flash 之间的关系

    前言:    在嵌入式开发中,如uboot的移植,kernel的移植都需要对Flash 有基本的了解.下面细说一下标题中的中Flash中的关系 一,Flash的内存存储结构    flash按照内部存 ...

  10. 《Pro Express.js》学习笔记——Express服务启动常规七步

    Express服务启动常规七步 1.       引用模块 var express=require('express'), compression=require('compression'), bo ...