发一个真正能运行的通过php生成的动态验证码图片的示例。网上下载的好多都报错的,于是自己综合各方面的见解,终于完成了这么一个效果。下面先发下效果图:

下面是php生成动态验证码需要用到的相关类和函数。

  1. <?php
  2. /**
  3. *ImageCode 生成包含验证码的GIF图片的函数
  4. *@param $string 字符串
  5. *@param $width 宽度
  6. *@param $height 高度
  7. **/
  8. function ImageCode($string='',$width=75,$height=25){
  9. $authstr=$string?$string:((time()%2==0)?mt_rand(1000,9999):mt_rand(10000,99999));
  10. $board_width=$width;
  11. $board_height=$height;
  12. // 生成一个32帧的GIF动画
  13. for($i=0;$i<32;$i++){
  14. ob_start();
  15. $image=imagecreate($board_width,$board_height);
  16. imagecolorallocate($image,0,0,0);
  17. // 设定文字颜色数组
  18. $colorList[]=ImageColorAllocate($image,15,73,210);
  19. $colorList[]=ImageColorAllocate($image,0,64,0);
  20. $colorList[]=ImageColorAllocate($image,0,0,64);
  21. $colorList[]=ImageColorAllocate($image,0,128,128);
  22. $colorList[]=ImageColorAllocate($image,27,52,47);
  23. $colorList[]=ImageColorAllocate($image,51,0,102);
  24. $colorList[]=ImageColorAllocate($image,0,0,145);
  25. $colorList[]=ImageColorAllocate($image,0,0,113);
  26. $colorList[]=ImageColorAllocate($image,0,51,51);
  27. $colorList[]=ImageColorAllocate($image,158,180,35);
  28. $colorList[]=ImageColorAllocate($image,59,59,59);
  29. $colorList[]=ImageColorAllocate($image,0,0,0);
  30. $colorList[]=ImageColorAllocate($image,1,128,180);
  31. $colorList[]=ImageColorAllocate($image,0,153,51);
  32. $colorList[]=ImageColorAllocate($image,60,131,1);
  33. $colorList[]=ImageColorAllocate($image,0,0,0);
  34. $fontcolor=ImageColorAllocate($image,0,0,0);
  35. $gray=ImageColorAllocate($image,245,245,245);
  36. $color=imagecolorallocate($image,255,255,255);
  37. $color2=imagecolorallocate($image,255,0,0);
  38. imagefill($image,0,0,$gray);
  39. $space=15;// 字符间距
  40. if($i>0){// 屏蔽第一帧
  41. $top=0;
  42. for($k=0;$k<strlen($authstr);$k++){
  43. $colorRandom=mt_rand(0,sizeof($colorList)-1);
  44. $float_top=rand(0,4);
  45. $float_left=rand(0,3);
  46. imagestring($image,6,$space*$k,$top+$float_top,substr($authstr,$k,1),$colorList[$colorRandom]);
  47. }
  48. }
  49. for($k=0;$k<20;$k++){
  50. $colorRandom=mt_rand(0,sizeof($colorList)-1);
  51. imagesetpixel($image,rand()%70,rand()%15,$colorList[$colorRandom]);
  52. }
  53. // 添加干扰线
  54. for($k=0;$k<3;$k++){
  55. $colorRandom=mt_rand(0,sizeof($colorList)-1);
  56. $todrawline=1;
  57. if($todrawline){
  58. imageline($image,mt_rand(0,$board_width),mt_rand(0,$board_height),mt_rand(0,$board_width),mt_rand(0,$board_height),$colorList[$colorRandom]);
  59. }else{
  60. $w=mt_rand(0,$board_width);
  61. $h=mt_rand(0,$board_width);
  62. imagearc($image,$board_width-floor($w / 2),floor($h / 2),$w,$h, rand(90,180),rand(180,270),$colorList[$colorRandom]);
  63. }
  64. }
  65. imagegif($image);
  66. imagedestroy($image);
  67. $imagedata[]=ob_get_contents();
  68. ob_clean();
  69. ++$i;
  70. }
  71. $gif=new GIFEncoder($imagedata);
  72. Header('Content-type:image/gif');
  73. echo $gif->GetAnimation();
  74. }
  75. /**
  76. *GIFEncoder类
  77. **/
  78. Class GIFEncoder{
  79. var $GIF="GIF89a";              /* GIF header 6 bytes       */
  80. var $VER="GIFEncoder V2.06";      /* Encoder version            */
  81. var $BUF=Array();
  82. var $LOP=0;
  83. var $DIS=2;
  84. var $COL=-1;
  85. var $IMG=-1;
  86. var $ERR=Array(
  87. 'ERR00'=>"Does not supported function for only one image!",
  88. 'ERR01'=>"Source is not a GIF image!",
  89. 'ERR02'=>"Unintelligible flag ",
  90. 'ERR03'=>"Could not make animation from animated GIF source",
  91. );
  92. function GIFEncoder($GIF_src,$GIF_dly=100,$GIF_lop=0,$GIF_dis=0, $GIF_red=0,$GIF_grn=0,$GIF_blu=0,$GIF_mod='bin'){
  93. if(!is_array($GIF_src)&&!is_array($GIF_tim)){
  94. printf("%s: %s",$this->VER,$this->ERR['ERR00']);
  95. exit(0);
  96. }
  97. $this->LOP=($GIF_lop>-1)?$GIF_lop:0;
  98. $this->DIS=($GIF_dis>-1)?(($GIF_dis<3)?$GIF_dis:3):2;
  99. $this->COL=($GIF_red>-1&&$GIF_grn>-1&&$GIF_blu>-1)?($GIF_red |($GIF_grn<<8)|($GIF_blu<<16)):-1;
  100. for($i=0,$src_count=count($GIF_src);$i<$src_count;$i++){
  101. if(strToLower($GIF_mod)=="url"){
  102. $this->BUF[]=fread(fopen($GIF_src [$i],"rb"),filesize($GIF_src [$i]));
  103. }elseif(strToLower($GIF_mod)=="bin"){
  104. $this->BUF [ ]=$GIF_src [ $i ];
  105. }else{
  106. printf("%s: %s(%s)!",$this->VER,$this->ERR [ 'ERR02' ],$GIF_mod);
  107. exit(0);
  108. }
  109. if(substr($this->BUF[$i],0,6)!="GIF87a"&&substr($this->BUF [$i],0,6)!="GIF89a"){
  110. printf("%s: %d %s",$this->VER,$i,$this->ERR ['ERR01']);
  111. exit(0);
  112. }
  113. for($j=(13+3*(2<<(ord($this->BUF[$i]{10})&0x07))),$k=TRUE;$k;$j++){
  114. switch($this->BUF [$i]{$j}){
  115. case "!":
  116. if((substr($this->BUF[$i],($j+3),8))=="NETSCAPE"){
  117. printf("%s: %s(%s source)!",$this->VER,$this->ERR ['ERR03'],($i+1));
  118. exit(0);
  119. }
  120. break;
  121. case ";":
  122. $k=FALSE;
  123. break;
  124. }
  125. }
  126. }
  127. GIFEncoder::GIFAddHeader();
  128. for($i=0,$count_buf=count($this->BUF);$i<$count_buf;$i++){
  129. GIFEncoder::GIFAddFrames($i,$GIF_dly[$i]);
  130. }
  131. GIFEncoder::GIFAddFooter();
  132. }
  133. function GIFAddHeader(){
  134. $cmap=0;
  135. if(ord($this->BUF[0]{10})&0x80){
  136. $cmap=3*(2<<(ord($this->BUF [0]{10})&0x07));
  137. $this->GIF.=substr($this->BUF [0],6,7);
  138. $this->GIF.=substr($this->BUF [0],13,$cmap);
  139. $this->GIF.="!\377\13NETSCAPE2.0\3\1".GIFEncoder::GIFWord($this->LOP)."\0";
  140. }
  141. }
  142. function GIFAddFrames($i,$d){
  143. $Locals_str=13+3*(2 <<(ord($this->BUF[$i]{10})&0x07));
  144. $Locals_end=strlen($this->BUF[$i])-$Locals_str-1;
  145. $Locals_tmp=substr($this->BUF[$i],$Locals_str,$Locals_end);
  146. $Global_len=2<<(ord($this->BUF [0]{10})&0x07);
  147. $Locals_len=2<<(ord($this->BUF[$i]{10})&0x07);
  148. $Global_rgb=substr($this->BUF[0],13,3*(2<<(ord($this->BUF[0]{10})&0x07)));
  149. $Locals_rgb=substr($this->BUF[$i],13,3*(2<<(ord($this->BUF[$i]{10})&0x07)));
  150. $Locals_ext="!\xF9\x04".chr(($this->DIS<<2)+0).chr(($d>>0)&0xFF).chr(($d>>8)&0xFF)."\x0\x0";
  151. if($this->COL>-1&&ord($this->BUF[$i]{10})&0x80){
  152. for($j=0;$j<(2<<(ord($this->BUF[$i]{10})&0x07));$j++){
  153. if(ord($Locals_rgb{3*$j+0})==($this->COL>> 0)&0xFF&&ord($Locals_rgb{3*$j+1})==($this->COL>> 8)&0xFF&&ord($Locals_rgb{3*$j+2})==($this->COL>>16)&0xFF){
  154. $Locals_ext="!\xF9\x04".chr(($this->DIS<<2)+1).chr(($d>>0)&0xFF).chr(($d>>8)&0xFF).chr($j)."\x0";
  155. break;
  156. }
  157. }
  158. }
  159. switch($Locals_tmp{0}){
  160. case "!":
  161. $Locals_img=substr($Locals_tmp,8,10);
  162. $Locals_tmp=substr($Locals_tmp,18,strlen($Locals_tmp)-18);
  163. break;
  164. case ",":
  165. $Locals_img=substr($Locals_tmp,0,10);
  166. $Locals_tmp=substr($Locals_tmp,10,strlen($Locals_tmp)-10);
  167. break;
  168. }
  169. if(ord($this->BUF[$i]{10})&0x80&&$this->IMG>-1){
  170. if($Global_len==$Locals_len){
  171. if(GIFEncoder::GIFBlockCompare($Global_rgb,$Locals_rgb,$Global_len)){
  172. $this->GIF.=($Locals_ext.$Locals_img.$Locals_tmp);
  173. }else{
  174. $byte=ord($Locals_img{9});
  175. $byte|=0x80;
  176. $byte&=0xF8;
  177. $byte|=(ord($this->BUF [0]{10})&0x07);
  178. $Locals_img{9}=chr($byte);
  179. $this->GIF.=($Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp);
  180. }
  181. }else{
  182. $byte=ord($Locals_img{9});
  183. $byte|=0x80;
  184. $byte&=0xF8;
  185. $byte|=(ord($this->BUF[$i]{10})&0x07);
  186. $Locals_img {9}=chr($byte);
  187. $this->GIF.=($Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp);
  188. }
  189. }else{
  190. $this->GIF.=($Locals_ext.$Locals_img.$Locals_tmp);
  191. }
  192. $this->IMG=1;
  193. }
  194. function GIFAddFooter(){
  195. $this->GIF.=";";
  196. }
  197. function GIFBlockCompare($GlobalBlock,$LocalBlock,$Len){
  198. for($i=0;$i<$Len;$i++){
  199. if($GlobalBlock{3*$i+0}!=$LocalBlock{3*$i+0}||$GlobalBlock{3*$i+1}!=$LocalBlock{3*$i+1}||$GlobalBlock{3*$i+2}!=$LocalBlock{3*$i+2}){
  200. return(0);
  201. }
  202. }
  203. return(1);
  204. }
  205. function GIFWord($int){
  206. return(chr($int&0xFF).chr(($int>>8)&0xFF));
  207. }
  208. function GetAnimation(){
  209. return($this->GIF);
  210. }
  211. }

php生成动态验证码用法示例(更多PHP教程请访问代码家园):

  1. /*调用示例*/
  2. session_start();
  3. $checkCode='';
  4. $chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ23456789';
  5. for($i=0;$i<4;$i++){
  6. $checkCode.=substr($chars,mt_rand(0,strlen($chars)-1),1);
  7. }
  8. $_SESSION['code']=strtoupper($checkCode);// 记录session
  9. ImageCode($checkCode,60);// 显示GIF动画

转载请注明:代码家园 » php生成动态验证码图片(gif)

本文原地址:http://www.daimajiayuan.com/sitejs-17105-1.html

转载于:https://www.cnblogs.com/sitejs/p/3461565.html

php生成GIF动态验证码图片(代码家园)相关推荐

  1. 生成数字相加验证码图片并验证

    Java代码随机生成数字相加验证码图片并验证 package com.xz.common;      import java.awt.Color;   import java.awt.Font;   ...

  2. 生成四位验证码php,PHP生成四位整数验证码图片及使用例子

    //checkimg.php 生成四位整数验证码图片 Header("Content-type: image/PNG"); srand((double)microtime()*10 ...

  3. 验证码生成类(可生成gif动态验证码)

    工具类一 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Bu ...

  4. python随机生成四位验证码的代码_Python random随机生成6位验证码示例代码

    随机生成6位验证码代码 # -*- coding: utf-8 -*- import random def generate_verification_code(): ''' randomly gen ...

  5. python生成4位验证码_Python 生成4位验证码图片

    import random import string from PIL import Image,ImageDraw,ImageFont,ImageFilter # 字体的位置 font_path ...

  6. php随机生成验证图片,php 生成随机验证码图片代码

    /** 默认首页 **/ class DefaultController extends AppController { public function index() { $len = 5; $st ...

  7. python生成海报商品图片_如何优雅的生成海报/动态合成图片 ?

    poster-generater ⚡⚡⚡海报生成器. 只需要一个简单的 json 配置即可生成你需要的海报... 说明 此项目诞生有一段时间了,我本人也一直在使用这个程序,从一开始的 golang 版 ...

  8. JS 即时刷新验证码图片代码

    此代码浏览器兼容性高 <script type="text/javascript"> <!-- // there's two ways get validate ...

  9. phpgif图片包_php生成动态验证码gif图片

    这是一个通过php生成的动态验证码图片的示例,重点是可以运行哦!下面先发下效果图: 下面是php生成动态验证码需要用到的相关类和函数. /** *ImageCode 生成包含验证码的GIF图片的函数 ...

  10. php验证码雪花源码,php如何生成雪花背景验证码(附代码)

    本文章来给大家介绍php生成雪花背景验证码程序代码 ,有需要的朋友可进入参考参考. 验证码生成程序<?php session_start(); session_register("lo ...

最新文章

  1. 单目和双目模式识别---游戏控制
  2. pytorch 单机多卡训练distributedDataParallel
  3. Python 一次for遍历多个列表及遍历时获取index
  4. java国际化——日期和时间+排序
  5. 你必须了解的session的本质
  6. java struts2 excel上传_文件上传方法,使用Struts2,实现Excel文件读取并写入数据库技术...
  7. 重磅快讯:CCF发布最新版推荐中文科技期刊目录
  8. linux ntp端口 修改,Linux时间配置(修改时间 修改日期 ntpd ntpdate NTP socket exiting)...
  9. [工具开发] 移动梦网短信发送客户端
  10. SylixOS 内存管理源代码分析--vmmMalloc.c
  11. StringIndexOutOfBoundsException
  12. 服务器添加hdr文件3dMax,别再乱用HDR了!关于它的10件事你该知道
  13. Web前端开发—html基础(3)
  14. 关于mis系统开发的一些配置含义
  15. 佐治亚理工计算机科学录取,佐治亚理工学院计算机科学排名第4(2018年TFE美国排名)...
  16. 由 Apache Kylin 组建的 Kyligence 公司获得数百万美元的天使轮投资
  17. 牛牛的跳跳棋(贪心)
  18. 与男友相爱7年的点滴,让我知道什么是好男人!(ZT)
  19. Redis的发布订阅
  20. 2020年冬小麦越冬期长势监测卫星遥感影像解读

热门文章

  1. 以围棋来说,人工智能程序跟通常程序差异在哪里
  2. mysql 隐秘后门_Phpstudy被暴存在隐藏后门-检查方法
  3. 查询某个分区是那张表_分库、分表、分区的区别
  4. php json schema,json-schema-php
  5. zxr10交换机配置手册vlan_最新中兴ZXR10交换机配置资料
  6. FindFirstFile
  7. epoll 和select/poll的区别
  8. [译] 在 Android 使用协程(part III) - 在实际工作中使用
  9. Kubernetes集群管理部署
  10. iOS开发之CoreSpotlight框架的应用