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

  1. <?php
  2. /**
  3. * __construct($new);构造函数创建一张图片$new->width['宽']->height['高']->r[r]->b[b]->g[g]
  4. * 添加数字 digital($digital); $digital->length['位数'] -> size['数字大小']
  5. * 添加字符串 font($font,$data) $font->length['位数'] -> size['数字大小'] $data需要的字符串不能是中文
  6. * 给验证码添加干扰元素 点 point($lenth) $lenth 多少个
  7. * 添加干扰元素 线 line($lenth) $lenth 多少个
  8. * function __destruct() 析构函数保存 session 打印图片 销毁图片
  9. */
  10. class TL_Captcha{
  11. private $image; //验证码图片
  12. private $width; //验证码宽度
  13. private $height; //验证码高度
  14. private $captch_code; //验证码信息
  15. /**
  16. * [__construct description] 构造函数创建一张图片
  17. * @param [type] $new [description] 图片大小 以及背景颜色 数组形式
  18. */
  19. public function __construct($new){
  20. session_start();
  21. $this->width = $new['width'];
  22. $this->height = $new['height'];
  23. //创建一张 宽$new['width'],高$new['height']的图片 [默认黑色]
  24. $this->image = imagecreatetruecolor($this->width, $this->height);
  25. //设置图片的底图 颜色为 r$new['r'],b$new['b'],g$new['g']
  26. $bgcolor = imagecolorallocate($this->image, $new['r'], $new['b'], $new['g']);
  27. //修改图片的底色
  28. imagefill($this->image,0,0,$bgcolor);
  29. }
  30. /**
  31. * 添加数字
  32. * @param [type] $digital [description] $digital数字配置 $digital['length'] 数字长度 $digital['size'] 数字大小
  33. * @return [type] [description]
  34. */
  35. public function digital($digital){
  36. for($i=0;$i<$digital['length'];$i++){
  37. //设置数字字体大小
  38. $fontsize = $digital['size'];
  39. //设置数字颜色
  40. $fontcolor = imagecolorallocate($this->image, rand(0,120), rand(0,120), rand(0,120));
  41. //设置数字的值
  42. $fontcontent = rand(0,9);
  43. //作用 保存 session
  44. $this->captch_code = $this->captch_code.$fontcontent;
  45. //设置放入图片的x轴
  46. $x = ($i*$this->width/$digital['length']) + rand(5,10);
  47. //设置放入图片的y轴
  48. $y = rand(5,10);
  49. //把数字放入图片
  50. imagestring($this->image, $fontsize, $x, $y, $fontcontent, $fontcolor);
  51. }
  52. }
  53. /**
  54. * 添加字符串
  55. * @param [type] $strings [description] $font数字配置 $strings['length'] 数字长度 $strings['size'] 数字大小
  56. * @param [type] $data [description] 字符串 不能有文字
  57. * @return [type] [description]
  58. */
  59. public function strings($strings,$data){
  60. for($i=0;$i<$strings['length'];$i++){
  61. //设置数字字体大小
  62. $fontsize = $strings['size'];
  63. //设置数字颜色
  64. $fontcolor = imagecolorallocate($this->image, rand(0,120), rand(0,120), rand(0,120));
  65. //截取字符串设置默认字符
  66. $fontcontent = substr($data, rand(0,strlen($data)),1);
  67. //作用 保存 session
  68. $this->captch_code = $this->captch_code.$fontcontent;
  69. //设置放入图片的x轴
  70. $x = ($i*$this->width/$strings['length']) + rand(5,10);
  71. //设置放入图片的y轴
  72. $y = rand(5,10);
  73. //把文字放入图片
  74. imagestring($this->image, $fontsize, $x, $y, $fontcontent, $fontcolor);
  75. }
  76. return $this->captch_code;
  77. }
  78. /**
  79. * 中文验证码
  80. * @param [type] $fontface [description] 文字路径
  81. * @param [type] $strdb [description] 随机的中文字符串
  82. * @param [type] $length [description] 验证码长度
  83. * @param [type] $size [description] 字体大小
  84. * @return [type] [description]
  85. */
  86. public function text($fontface,$strdb,$length,$size){
  87. //转换数组
  88. $strdbs = str_split($strdb,3);
  89. for($i=0;$i<$length;$i++){
  90. //设置文字颜色
  91. $fontcolor = imagecolorallocate($this->image, rand(0,120), rand(0,120), rand(0,120));
  92. //获取随机数组中的数字
  93. $cn = $strdbs[rand(0,count($strdbs)-1)];
  94. //写入session
  95. $this->captch_code = $this->captch_code.$cn;
  96. $x = (($i*$this->width/$length))+rand(5,10);
  97. $y = rand(30,35);
  98. imagettftext($this->image, $size, mt_rand(-60,60), $x, $y, $fontcolor, $fontface, $cn);
  99. }
  100. }
  101. /**
  102. * 给验证码添加干扰元素 点
  103. * @param [type] $lenth [description] 添加点的数量
  104. * @return [type] [description]
  105. */
  106. public function point($lenth){
  107. for($i=0;$i<$lenth;$i++){
  108. //设置点的颜色
  109. $pointcolor = imagecolorallocate($this->image, rand(50,200), rand(50,200), rand(50,200));
  110. //放入验证码
  111. imagesetpixel($this->image, rand(1,$this->width), rand(1,$this->height), $pointcolor);
  112. }
  113. }
  114. /**
  115. * 添加干扰元素 线
  116. * @param [type] $lenth [description] 添加点的数量
  117. * @return [type] [description]
  118. */
  119. public function line($lenth){
  120. for($i=0;$i<$lenth;$i++){
  121. //设置线的颜色
  122. $linecolor = imagecolorallocate($this->image, rand(80,220), rand(80,220), rand(80,220));
  123. //放入验证码
  124. imageline($this->image,rand(1,$this->width),rand(1,$this->height),rand(1,$this->width),rand(1,$this->height),$linecolor);
  125. }
  126. }
  127. /**
  128. * 构造函数打印图片并且销毁
  129. */
  130. public function __destruct() {
  131. header('content-type:image/png');
  132. //创建SESSION
  133. $_SESSION['authcode'] = $this->captch_code;
  134. //打印图片
  135. imagepng($this->image);
  136. //销毁图片
  137. imagedestroy($this->image);
  138. }
  139. }
  140. $new = array(
  141. 'width' => 200,
  142. 'height' => 50,
  143. 'r' => 255,
  144. 'b' => 255,
  145. 'g' => 255
  146. );
  147. $Captcha = new Captcha($new);
  148. //随机数字
  149. // $digital = array(
  150. // 'length' => 5,
  151. // 'size' => 6,
  152. // );
  153. // $Captcha->digital($digital);
  154. //
  155. //随机字符串 不能有中文
  156. // $strings = array(
  157. // 'length' => 5,
  158. // 'size' => 6,
  159. // );
  160. // $data = "qweqwewqewqeewqe1545616545446";
  161. // $Captcha->strings($strings,$data);
  162. //随机文字验证吗
  163. //字体文件
  164. // $fontface = "STXINGKA.TTF";
  165. // $strdb = '晨起微风吹拂迎着第一缕朝阳绽放的方向踩着清凉的露珠沐着微醉的晨风静静漫行在清爽舒适的田野上欣喜盈怀云很轻风很静万物生灵大多还沉浸在黎明前的宁静里酣睡只有几只晨起的粉蝶在花中轻舞寂静的清晨给人一种恬淡安然温润祥和的柔美韵致令人陶醉其中流恋忘返沿着那条潺潺流动的小溪蜿蜒而下聆听溪水欢快地吟唱心变得无比的愉悦慢行几步稍加留意就能看到小鱼们在浅水里欢乐地舞蹈顽皮的小虾们在溪边的水草丛里追逐嬉戏若是你够幸运的话兴许还能撞上小乌龟在堤脚下的缝隙间探出半个头来欣喜地打量着这个神奇的世界呢然而这静好的一切随着一群大白鹅和一群小花鸭的介入而结束在鹅与鸭的叫声响起的一刹那小鱼小虾们早已屏声静气地藏匿好了身影那胆小怕事的小乌龟更是吓得立马缩回那才稍稍探出小半截的头颅瞬间整条小溪便盈满了鹅与鸭的嘹亮歌声';
  166. // $length = "5";
  167. // $Captcha->text($fontface,$strdb,$length,"14");
  168. //加干扰元素 点
  169. // $Captcha->point('200');
  170. //加干扰元素线
  171. // $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 3206: [Apio2013]道路费用【最小生成树+并查集】

    参考:http://hzwer.com/6888.html 把k条道路权值设为0,和其他边一起跑MST,然后把此时选中的其他边设为必选,在新图中加上必选变缩成k个点,把所有边重标号,枚举k跳边的选取情 ...

  2. Centos 7 chrome

    share from https://www.cnblogs.com/lenmom/p/9195581.html 1. 下载Chrome浏览器的rpm包 https://www.chrome64bit ...

  3. ntp多台主机时间同步

    通俗的讲,多台主机ntp时间同步,就是自定义集群中一台机器(我们这里叫它server)与网络时间同步,然后其它主机与server主机时间同步 另外,ntp时间同步机制不是我们想象的那样直接同步,而是“ ...

  4. 【css】rem及其替换方案

    移动端的web前端开发其实经常会有一些令人头疼的问题,比如屏幕适配.1像素问题等,rem也是之前在屏幕适配上比较完善的一套方案,但是随着业务的深入,任何方案都有其优秀与不足的地方,rem这套方案也一样 ...

  5. Elixir安装

    参考:https://laravel.com/docs/5.2/elixir 1. 安装node 去这里下载 2.可以用淘宝的cnpm加速! npm install -g cnpm --registr ...

  6. 一个iOS开发者的修真之路

    在微信上有童鞋问我iOS开发者的入门标准是神马?这个问题难到我了,而且贸然给一个答案出来的话,必定会有万千高手来喷. 凡人修仙,仙人修道,道人修真.当我们还是一个在青石板上蹲马步汗水涔涔的废柴时,或许 ...

  7. 关于OPPO手机的生存和程序员的发展

    关于程序员私下讨论最多的话题,除了哪个编程最牛逼之外,哪款品牌的手机最牛逼也是我们谈论最多的话题之一吧!有的喜欢罗永浩,自然就是锤粉:有的喜欢苹果,称它为工业时代最优美的艺术品:当然,我想也有很多的人 ...

  8. 关于表单清空的细节(reset函数或者class="reset"属性)

    在需要清空的表单的情况下, 如果是在页面中 那么就添加属性 class="reset"  也即是 <button class="reset" value= ...

  9. AJPFX详解泛型中super和extends关键字

    首先,我们定义两个类,A和B,并且假设B继承自A.下面的代码中,定义了几个静态泛型方法,这几个例子随便写的,并不是特别完善,我们主要考量编译失败的问题: Java代码  public class Ge ...

  10. UnixTime的时间戳的转换

    public static string XConvertDateTime(double unixTime) { System.DateTime time = System.DateTime.MinV ...