封装captcha类 -- 画图四
<?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类 -- 画图四的更多相关文章
- 领域模型中的实体类分为四种类型:VO、DTO、DO、PO
http://kb.cnblogs.com/page/522348/ 由于不同的项目和开发人员有不同的命名习惯,这里我首先对上述的概念进行一个简单描述,名字只是个标识,我们重点关注其概念: 概念: V ...
- 封装application类
<?php //判断用户是否是通过入口文件访问 if(!defined('ACCESS')){ echo '非法请求'; die; } //封装初始化类 cla ...
- PHP连接数据库:封装成类
php连接数据库,操作他增删改查等操作,其中要多次连接数据库,每个页面也需要连接数据库,更改数据会及其麻烦: 为了便于数据库的更改,我们可以把固定的那几句话封装成类,这样虽然代码量也差不多,但是有利于 ...
- 域模型中的实体类分为四种类型:VO、DTO、DO、PO
经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度,对这几个概念进行简析. 得出的主要结论是:在项目应用中,VO对应于页面上需要显示的数据(表单),DO对应 ...
- c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)
c#封装DBHelper类 public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...
- php使用GD库实现图片水印和缩略图——封装成类
学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来 ...
- 适用于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通用)>,现在重新整理一 ...
- 转:领域模型中的实体类分为四种类型:VO、DTO、DO、PO
经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度,对这几个概念进行简析.得出的主要结论是:在项目应用中,VO对应于页面上需要显示的数据(表单),DO对应于 ...
- 孟老板 ListAdapter封装, 告别Adapter代码 (四)
BaseAdapter系列 ListAdapter封装, 告别Adapter代码 (一) ListAdapter封装, 告别Adapter代码 (二) ListAdapter封装, 告别Adapter ...
随机推荐
- hibernate hql
hibernate在使用hql进行select count(*) from ObjectA left join fetch apath 时会报错,多余的left join去掉即可.
- STM32 串口固件库中定义的几个中断标志位什么意思?
在stm32f10x_usart.h中以上几个宏,很没有规律,诈一看还真不知道为什么会这么定义,其实通过代码就很容易明白: D7~D5:代表中断标志位对应的中断使能位在 CR1.CR2还是CR3寄存器 ...
- virtualbox虚拟机中的centos与macos共享文件夹
开发中需要用到linux环境,所以使用共享模式开发.通过samba服务器来实现. 环境: 虚拟机 virtualbox 虚拟系统 centos 6.6 本机 macos 192.168.1.102 ...
- 改善C#程序,提高程序运行效率的50种方法
改善C#程序,提高程序运行效率的50种方法 转自:http://blog.sina.com.cn/s/blog_6f7a7fb501017p8a.html 一.用属性代替可访问的字段 1..NET ...
- 关于头文件的一些常用<meta>
一.常见的<meta>(摘自百度) 1. 设置编码信息<meta http-equiv="Content-Type" Content="text/htm ...
- 转载:ViewHolder为什么声明为static
转自:http://www.cnblogs.com/bluestorm/p/5867061.html ListView优化中ViewHolder要不要定义为static静态内部类? 给学生讲课的时 ...
- android 中调用接口发送短信
android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...
- angularJS ngClass如何使用
<!doctype html> <html ng-app="firstApp"> <head> <meta charset="u ...
- NoSQL生态系统——hash分片和范围分片两种分片
13.4 横向扩展带来性能提升 很多NoSQL系统都是基于键值模型的,因此其查询条件也基本上是基于键值的查询,基本不会有对整个数据进行查询的时候.由于基本上所有的查询操作都是基本键值形式的,因此分片通 ...
- 在EC2上搭建L2TP over IPSec VPN服务器
注意(:wq保存文件 putty登陆用户名为ec2-user) 安装与配置: 环境介绍: OS:CentOS 6.4 x86_64 Minimal 1. 修改 /etc/sysctl.conf,新增如 ...