文章来源:http://www.cnblogs.com/hello-tl/p/7592998.html

<?php
/**
* __construct($new);构造函数创建一张图片$new->width['宽']->height['高']->r[r]->b[b]->g[g]
* 添加数字 digital($digital); $digital->length['位数'] -> size['数字大小']
* 添加字符串 font($font,$data) $font->length['位数'] -> size['数字大小'] $data需要的字符串不能是中文
* 给验证码添加干扰元素 点 point($lenth) $lenth 多少个
* 添加干扰元素 线 line($lenth) $lenth 多少个
* function __destruct() 析构函数保存 session 打印图片 销毁图片
*/
class TL_Captcha{
private $image; //验证码图片
private $width; //验证码宽度
private $height; //验证码高度
private $captch_code; //验证码信息
/**
* [__construct description] 构造函数创建一张图片
* @param [type] $new [description] 图片大小 以及背景颜色 数组形式
*/
public function __construct($new){
session_start();
$this->width = $new['width'];
$this->height = $new['height'];
//创建一张 宽$new['width'],高$new['height']的图片 [默认黑色]
$this->image = imagecreatetruecolor($this->width, $this->height);
//设置图片的底图 颜色为 r$new['r'],b$new['b'],g$new['g']
$bgcolor = imagecolorallocate($this->image, $new['r'], $new['b'], $new['g']);
//修改图片的底色
imagefill($this->image,0,0,$bgcolor);
}
/**
* 添加数字
* @param [type] $digital [description] $digital数字配置 $digital['length'] 数字长度 $digital['size'] 数字大小
* @return [type] [description]
*/
public function digital($digital){
for($i=0;$i<$digital['length'];$i++){
//设置数字字体大小
$fontsize = $digital['size'];
//设置数字颜色
$fontcolor = imagecolorallocate($this->image, rand(0,120), rand(0,120), rand(0,120));
//设置数字的值
$fontcontent = rand(0,9);
//作用 保存 session
$this->captch_code = $this->captch_code.$fontcontent;
//设置放入图片的x轴
$x = ($i*$this->width/$digital['length']) + rand(5,10);
//设置放入图片的y轴
$y = rand(5,10);
//把数字放入图片
imagestring($this->image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
}
/**
* 添加字符串
* @param [type] $strings [description] $font数字配置 $strings['length'] 数字长度 $strings['size'] 数字大小
* @param [type] $data [description] 字符串 不能有文字
* @return [type] [description]
*/
public function strings($strings,$data){
for($i=0;$i<$strings['length'];$i++){
//设置数字字体大小
$fontsize = $strings['size'];
//设置数字颜色
$fontcolor = imagecolorallocate($this->image, rand(0,120), rand(0,120), rand(0,120));
//截取字符串设置默认字符
$fontcontent = substr($data, rand(0,strlen($data)),1);
//作用 保存 session
$this->captch_code = $this->captch_code.$fontcontent;
//设置放入图片的x轴
$x = ($i*$this->width/$strings['length']) + rand(5,10);
//设置放入图片的y轴
$y = rand(5,10);
//把文字放入图片
imagestring($this->image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
return $this->captch_code;
}
/**
* 中文验证码
* @param [type] $fontface [description] 文字路径
* @param [type] $strdb [description] 随机的中文字符串
* @param [type] $length [description] 验证码长度
* @param [type] $size [description] 字体大小
* @return [type] [description]
*/
public function text($fontface,$strdb,$length,$size){
//转换数组
$strdbs = str_split($strdb,3);
for($i=0;$i<$length;$i++){
//设置文字颜色
$fontcolor = imagecolorallocate($this->image, rand(0,120), rand(0,120), rand(0,120));
//获取随机数组中的数字
$cn = $strdbs[rand(0,count($strdbs)-1)];
//写入session
$this->captch_code = $this->captch_code.$cn;
$x = (($i*$this->width/$length))+rand(5,10);
$y = rand(30,35);
imagettftext($this->image, $size, mt_rand(-60,60), $x, $y, $fontcolor, $fontface, $cn);
}
}
/**
* 给验证码添加干扰元素 点
* @param [type] $lenth [description] 添加点的数量
* @return [type] [description]
*/
public function point($lenth){
for($i=0;$i<$lenth;$i++){
//设置点的颜色
$pointcolor = imagecolorallocate($this->image, rand(50,200), rand(50,200), rand(50,200));
//放入验证码
imagesetpixel($this->image, rand(1,$this->width), rand(1,$this->height), $pointcolor);
}
}
/**
* 添加干扰元素 线
* @param [type] $lenth [description] 添加点的数量
* @return [type] [description]
*/
public function line($lenth){
for($i=0;$i<$lenth;$i++){
//设置线的颜色
$linecolor = imagecolorallocate($this->image, rand(80,220), rand(80,220), rand(80,220));
//放入验证码
imageline($this->image,rand(1,$this->width),rand(1,$this->height),rand(1,$this->width),rand(1,$this->height),$linecolor);
}
}
/**
* 构造函数打印图片并且销毁
*/
public function __destruct() {
header('content-type:image/png');
//创建SESSION
$_SESSION['authcode'] = $this->captch_code;
//打印图片
imagepng($this->image);
//销毁图片
imagedestroy($this->image);
}
}
$new = array(
'width' => 200,
'height' => 50,
'r' => 255,
'b' => 255,
'g' => 255
);
$Captcha = new Captcha($new);
//随机数字
// $digital = array(
// 'length' => 5,
// 'size' => 6,
// );
// $Captcha->digital($digital);
//
//随机字符串 不能有中文
// $strings = array(
// 'length' => 5,
// 'size' => 6,
// );
// $data = "qweqwewqewqeewqe1545616545446";
// $Captcha->strings($strings,$data);
//随机文字验证吗
//字体文件
// $fontface = "STXINGKA.TTF";
// $strdb = '晨起微风吹拂迎着第一缕朝阳绽放的方向踩着清凉的露珠沐着微醉的晨风静静漫行在清爽舒适的田野上欣喜盈怀云很轻风很静万物生灵大多还沉浸在黎明前的宁静里酣睡只有几只晨起的粉蝶在花中轻舞寂静的清晨给人一种恬淡安然温润祥和的柔美韵致令人陶醉其中流恋忘返沿着那条潺潺流动的小溪蜿蜒而下聆听溪水欢快地吟唱心变得无比的愉悦慢行几步稍加留意就能看到小鱼们在浅水里欢乐地舞蹈顽皮的小虾们在溪边的水草丛里追逐嬉戏若是你够幸运的话兴许还能撞上小乌龟在堤脚下的缝隙间探出半个头来欣喜地打量着这个神奇的世界呢然而这静好的一切随着一群大白鹅和一群小花鸭的介入而结束在鹅与鸭的叫声响起的一刹那小鱼小虾们早已屏声静气地藏匿好了身影那胆小怕事的小乌龟更是吓得立马缩回那才稍稍探出小半截的头颅瞬间整条小溪便盈满了鹅与鸭的嘹亮歌声';
// $length = "5";
// $Captcha->text($fontface,$strdb,$length,"14");
//加干扰元素 点
// $Captcha->point('200');
//加干扰元素线
// $Captcha->line('5');

文章来源:http://www.cnblogs.com/hello-tl/p/7592998.html

PHP:GD库 生成验证码图片的更多相关文章

  1. 利用php的GD库生成验证码

    <?php ,); //创建一个100宽30高的底图,默认黑色 ,,); //修改颜色.数字对应 rgb 的三个数值.白色 imagefill(,,$bgcolor); //从左上角到右下角把颜 ...

  2. PHP5 GD库生成图形验证码(汉字)

    PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...

  3. java web学习总结(九) -------------------通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  4. JavaWeb---总结(九)通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片  1 package gacl. ...

  5. Java 生成验证码图片

    生成验证码图片并对提交的输入进行验证 // HttpServletResponse常见应用——生成验证码 // 利用BufferedImage类生产随机图片 public static final i ...

  6. javaweb学习总结(九)—— 通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  7. 012. asp.net生成验证码图片(汉字示例/字母+数字)

    protected void Page_Load(object sender, EventArgs e) { //生成验证码图片的基本步骤 string checkCode = "新年快乐& ...

  8. J2EE如何生成验证码图片和点击刷新验证码

    验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...

  9. java web 学习九(通过servlet生成验证码图片)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

随机推荐

  1. bzoj 4951: [Wf2017]Money for Nothing【分治】

    参考:https://blog.csdn.net/herobrine_tkj/article/details/78404426?locationNum=8&fps=1 为什么从1开始存就挂了, ...

  2. mysql主从同步异常原因及恢复

    mysql主从同步异常原因及恢复 前言 mysql数据库做主从复制,不仅可以为数据库的数据做实时备份,保证数据的完整性,还能做为读写分离,提升数据库的整体性能.但是,mysql主从复制经常会因为某些原 ...

  3. vijos1846 [NOIP2013] 华容道【最短路】

    传送门:https://vijos.org/p/1983 (其实noip的题各个oj都会有的,就不贴其它传送门了) 这道题真的是,怎么说,我都不知道怎么评价了= =.果然数据量小的题怎么暴力都可以过. ...

  4. 修改SolrCloud在ZooKeeper中的配置文件操作记录

    修改SolrCloud在ZooKeeper中的配置文件操作记录. 命令执行目录: /opt/solr-/server/scripts/cloud-scripts/ 1.下载配置文件 ./zkcli., ...

  5. Android Dialogs(3)警示Dialog教程[创建,单选,复选,自定义]等等

    本节内容 1. Building an Alert Dialog 2. Adding buttons 3. Adding a list 4. Adding a persistent multiple- ...

  6. 把sed当作命令解释器使用

    [root@sishen ~]# vim script.sed #!/bin/sed -f #交换第一列和第二列 s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1, \3/g ...

  7. 给定一个整数 n,返回 n! 结果尾数中零的数量。

    示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120, 尾数中有 1 个零. 代码部分 class Solution ...

  8. jquery基础知识点总结

    Jquery是一个优秀的js库,它简化了js的复杂操作,不需要关心浏览器的兼容问题,提供了大量实用方法. Jquery的写法 方法函数化 链式操作 取值赋值合体] $(“p”).html();   取 ...

  9. ie浏览器和火狐浏览器对对容器宽度定义的差异

    首先我们说说firefox和IE对CSS的宽度显示有什么不同: 其实CSS ’width’ 指的是标准CSS中所指的width的宽度,在firefox中的宽度就是这个宽度.它只包含容器中内容的宽度.而 ...

  10. Android一句代码给Activity定制标题栏

    在此之前,使用过几种方法设置标题栏: 1.常规法:这个方法是最常用的了,哪个activity需要什么样的标题栏,就在对应的xml布局设计.缺点:此方法维护起来困难,没有将标题栏的共性抽取出来, 如果要 ...