phpgif图片包_PHP生成GIF动态图片验证码

1 <?php2 /**3 * 调用示例4 **/

5 session_start();6 $randCode = '';7 //验证码随机

8 $str="abcdefghjkmnpqrstuvwsyzABCDEFGHJKMNPQRSTUVWSYZ23456789";9 for($i=0;$i<4;$i++){10 $safe.=substr($str,rand(0,strlen($str)),1);11 }12 $_SESSION["imgcode"]=$safe;13 ImageCode($safe, 60); //显示GIF动画

14

15 /**16 * ImageCode 生成GIF图片验证17 * @param $string 字符串18 * @param $width 宽度19 * @param $height 高度20 **/

21 function ImageCode($string = '', $width = 75, $height = 25){22 $authstr = $string ? $string : ((time() % 2 == 0) ? mt_rand(1000, 9999) : mt_rand(10000, 99999));23 $board_width = $width;24 $board_height = $height;25 //生成一个32帧的GIF动画

26 for($i = 0; $i < 32; $i++){27 ob_start();28 $image = imagecreate($board_width, $board_height);29 imagecolorallocate($image, 0,0,0);30 //设定文字颜色数组

31 $colorList[] = ImageColorAllocate($image, 15,73,210);32 $colorList[] = ImageColorAllocate($image, 0,64,0);33 $colorList[] = ImageColorAllocate($image, 0,0,64);34 $colorList[] = ImageColorAllocate($image, 0,128,128);35 $colorList[] = ImageColorAllocate($image, 27,52,47);36 $colorList[] = ImageColorAllocate($image, 51,0,102);37 $colorList[] = ImageColorAllocate($image, 0,0,145);38 $colorList[] = ImageColorAllocate($image, 0,0,113);39 $colorList[] = ImageColorAllocate($image, 0,51,51);40 $colorList[] = ImageColorAllocate($image, 158,180,35);41 $colorList[] = ImageColorAllocate($image, 59,59,59);42 $colorList[] = ImageColorAllocate($image, 0,0,0);43 $colorList[] = ImageColorAllocate($image, 1,128,180);44 $colorList[] = ImageColorAllocate($image, 0,153,51);45 $colorList[] = ImageColorAllocate($image, 60,131,1);46 $colorList[] = ImageColorAllocate($image, 0,0,0);47 $fontcolor = ImageColorAllocate($image, 0,0,0);48 $gray = ImageColorAllocate($image, 245,245,245);49 $color = imagecolorallocate($image, 255,255,255);50 $color2 = imagecolorallocate($image, 255,0,0);51 imagefill($image, 0, 0, $gray);52 $space = 15;//字符间距

53 if($i > 0){ //屏蔽第一帧

54 for ($k = 0; $k < strlen($authstr); $k++) {55 $colorRandom = mt_rand(0,sizeof($colorList)-1);56 $float_top = rand(0,4);57 $float_left = rand(0,3);58 imagestring($image, 6, $space * $k, $top + $float_top, substr($authstr, $k, 1), $colorList[$colorRandom]);59 }60 }61 for ($k = 0; $k < 20; $k++) {62 $colorRandom = mt_rand(0,sizeof($colorList)-1);63 imagesetpixel($image, rand()%70 , rand()%15 , $colorList[$colorRandom]);64

65 }66 //添加干扰线

67 for($k = 0; $k < 5; $k++){68 $colorRandom = mt_rand(0, sizeof($colorList)-1);69 //$todrawline = rand(0,1);

70 $todrawline = 1;71 if($todrawline){72 imageline($image, mt_rand(0, $board_width), mt_rand(0,$board_height), mt_rand(0,$board_width), mt_rand(0,$board_height), $colorList[$colorRandom]);73 }else{74 $w = mt_rand(0,$board_width);75 $h = mt_rand(0,$board_width);76 imagearc($image, $board_width - floor($w / 2) , floor($h / 2), $w, $h, rand(90,180), rand(180,270), $colorList[$colorRandom]);77 }78 }79 imagegif($image);80 imagedestroy($image);81 $imagedata[] = ob_get_contents();82 ob_clean();83 ++$i;84 }85 $gif = new GIFEncoder($imagedata);86 Header ('Content-type:image/gif');87 echo $gif->GetAnimation();88 }89

90

91 /**92 * GIFEncoder类93 **/

94 classGIFEncoder {95 var $GIF = "GIF89a"; /*GIF header 6 bytes*/

96 var $VER = "GIFEncoder V2.06"; /*Encoder version*/

97 var $BUF = Array( );98 var $LOP = 0;99 var $DIS = 2;100 var $COL = -1;101 var $IMG = -1;102 var $ERR = Array(103 'ERR00' =>"Does not supported function for only one image!",

104 'ERR01' =>"Source is not a GIF image!",

105 'ERR02' =>"Unintelligible flag ",

106 'ERR03' =>"Could not make animation from animated GIF source",

107 );108 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') {109 if (!is_array($GIF_src) && !is_array($GIF_tim)) {110 printf ( "%s: %s", $this->VER, $this->ERR['ERR00']);111 exit( 0);112 }113 $this->LOP = ($GIF_lop > -1) ? $GIF_lop : 0;114 $this->DIS = ($GIF_dis > -1) ? (( $GIF_dis < 3 ) ? $GIF_dis : 3) : 2;115 $this->COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ? ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;116

117 for ($i = 0, $src_count = count($GIF_src); $i < $src_count; $i++) {118 if (strToLower( $GIF_mod ) == "url") {119 $this->BUF[] = fread (fopen($GIF_src [$i], "rb"), filesize ($GIF_src [$i]));120 }elseif(strToLower($GIF_mod) == "bin") {121 $this->BUF [ ] = $GIF_src [ $i];122 }else{123 printf("%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod);124 exit(0);125 }126 if (substr($this->BUF[$i], 0, 6) != "GIF87a" && substr($this->BUF [$i], 0, 6) != "GIF89a") {127 printf( "%s: %d %s", $this->VER, $i, $this->ERR ['ERR01']);128 exit(0);129 }130 for ($j = (13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07 ))), $k = TRUE; $k; $j++){131 switch ($this->BUF [$i]{$j}) {132 case "!":

133 if ((substr($this->BUF[$i], ($j + 3), 8)) == "NETSCAPE") {134 printf( "%s: %s ( %s source )!", $this->VER, $this->ERR ['ERR03'], ($i + 1));135 exit( 0);136 }137 break;138 case ";":

139 $k = FALSE;140 break;141 }142 }143 }144 GIFEncoder::GIFAddHeader();145 for($i = 0, $count_buf = count($this->BUF); $i < $count_buf; $i++) {146 GIFEncoder::GIFAddFrames($i, $GIF_dly[$i]);147 }148 GIFEncoder::GIFAddFooter();149 }150

151 functionGIFAddHeader ( ) {152 $cmap = 0;153 if (ord($this->BUF[0]{10}) & 0x80) {154 $cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ]{10} ) & 0x07));155

156 $this->GIF .= substr ( $this->BUF [ 0 ], 6, 7);157 $this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap);158 $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFEncoder::GIFWord ( $this->LOP ) . "\0";159 }160 }161

162 function GIFAddFrames ( $i, $d) {163 $Locals_str = 13 + 3 * (2 <BUF[$i]{10}) & 0x07));164 $Locals_end = strlen($this->BUF[$i]) - $Locals_str - 1;165 $Locals_tmp = substr ($this->BUF[$i], $Locals_str, $Locals_end);166 $Global_len = 2 << (ord( $this->BUF [0]{10} ) & 0x07);167 $Locals_len = 2 << (ord( $this->BUF[$i]{10}) & 0x07);168 $Global_rgb = substr($this->BUF[0], 13, 3 * (2 << ( ord ( $this->BUF[0]{10} ) & 0x07)));169 $Locals_rgb = substr ( $this->BUF[$i], 13, 3 * (2 << ( ord ( $this->BUF[$i]{10} ) & 0x07)));170 $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 0) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";171 if ( $this->COL > -1 && ord($this->BUF[$i]{10}) & 0x80){172 for($j = 0; $j < (2 << (ord( $this->BUF[$i]{10}) & 0x07)); $j++) {173 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) {174 $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 1) . chr (( $d >> 0) & 0xFF) . chr (( $d >> 8) & 0xFF) . chr ($j) . "\x0";175 break;176 }177 }178 }179 switch ( $Locals_tmp { 0} ) {180 case "!":

181 $Locals_img = substr($Locals_tmp, 8, 10);182 $Locals_tmp = substr($Locals_tmp, 18, strlen ($Locals_tmp) - 18);183 break;184 case ",":

185 $Locals_img = substr($Locals_tmp, 0, 10);186 $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);187 break;188 }189 if ( ord ( $this->BUF[$i]{10} ) & 0x80 && $this->IMG > -1) {190 if ( $Global_len == $Locals_len) {191 if ( GIFEncoder::GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len) ) {192 $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp);193 }else{194 $byte = ord ( $Locals_img{9});195 $byte |= 0x80;196 $byte &= 0xF8;197 $byte |= ( ord ( $this->BUF [ 0 ]{10}) & 0x07);198 $Locals_img{9} = chr($byte);199 $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);200 }201 }else{202 $byte = ord($Locals_img{9});203 $byte |= 0x80;204 $byte &= 0xF8;205 $byte |= (ord($this->BUF[$i]{10}) & 0x07);206 $Locals_img {9} = chr($byte);207 $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);208 }209 }else{210 $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp);211 }212 $this->IMG = 1;213 }214

215 functionGIFAddFooter ( ) {216 $this->GIF .= ";";217 }218

219 function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len) {220 for ( $i = 0; $i < $Len; $i++) {221 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}) {222 return ( 0);223 }224 }225 return ( 1);226 }227

228 function GIFWord ( $int) {229 return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF) );230 }231

232 functionGetAnimation ( ) {233 return ($this->GIF);234 }235 }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/530354.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

工程图标注粗糙度_Inventor教程之工程图标注实例

1工程图标注实例对以下实体零件进行全部的标注演示。操作步骤如下&#xff1a;(1)打开文件。运行Inventor&#xff0c;单击“快速入门”选项卡“启动”面板上的“打开”按钮&#xff0c;在“打开”对话框中选择“实体零件”&#xff0c;单击“打开”按钮进入实体零件。(2)新建工…

Oracle数据库 invalid character问题解决

今天使用PL/SQL Developer这个工具来操作Oracle数据时发现了一个问题&#xff1a; select * from tb_student_grade pivot(max(grade) for course in(math as 数学,chinese as 语文,english as 英语)); 执行这个SQL语句提示invalid character,原因是我的数据库编码是AMERICAN…

定时线程_SpringBoot定时任务,@Async多线程异步执行

一、使用SpringBoot实现定时任务这个不是重点&#xff0c;就简单的实现一下&#xff0c;至于cron表达式怎么写也不是重点&#xff0c;自行百度即可。1-1、基于 Scheduled 注解的方式import org.springframework.scheduling.annotation.EnableScheduling;import org.springframe…

SpringBoot入门一

SpringBoot能够很简单的创建一个直接运行的单体Spring应用 特性&#xff1a; 单体Spring应用内置的tomcat、Jetty提供默认的starter来构建配置自动配置Spring和第三方库 推荐一个很好的学习教程&#xff0c;https://blog.csdn.net/u010486495/article/details/79348302 1 构…

mysql怎么把datetime类型转换_mysql怎样实现time转datetime

mysql实现time转datetime的方法&#xff1a;使用在sql语句中【FROM_UNIXTIME(时间值)】&#xff0c;代码为【insert into test(time) values(FROM_UNIXTIME(%d))",time(NULL)】。mysql实现time转datetime的方法&#xff1a;FROM_UNIXTIME(time(NULL))将liunx系统的time_t类…

SpringBoot入门二

参考Spring Boot Starters - 御坂研究所 创建自己的starter starter是依赖的一种synthesize&#xff08;合成&#xff09;。 starter会把需要用到的依赖全部包含进来&#xff0c;避免开发者自己手动引入依赖。 starter的逻辑 pom.xml<parent><groupId>org.spri…

Tomcat入门

一&#xff0c;tomcat启动 双击startup.bat,如果出现一闪而过的情况&#xff0c;在文件的末尾添加pause&#xff0c;就可以看到环境变量设置的路径是否正确 如果无法在电脑的高级系统设置中设置环境变量&#xff0c;可以在setclasspath.bat中设置环境变量 set JAVA_HOMEC:\P…

php mysql 图像_将图像插入MySQL并使用PHP检索图像

此文可能比较繁琐&#xff0c;有更好的方法&#xff0c;但是出于教程目的&#xff0c;这是我的"“最佳实践”的路线。今天&#xff0c;我们将讨论一个似乎每个人都有些困惑的话题……在MySQL中存储BLOB图像&#xff0c;然后使用PHP再次显示它们。尽管始终建议不要这样做&a…

利用Maven逆向工程生成mybatis映射文件

一&#xff0c;pom.xml 注意修改逆向工程配置文件的路径 <build><pluginManagement><plugins><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1…

mysql update多个表_mysql update 多表 (复制)

定我们有两张表&#xff0c;一张表为Product表存放产品信息&#xff0c;其中有产品价格列Price&#xff1b;另外一张表是ProductPrice表&#xff0c;我们要将ProductPrice表中的价格字段Price更新为Price表中价格字段的80%。在Mysql中我们有几种手段可以做到这一点&#xff0c;…

ORA-00907:missing right parenthesis缺少右括号

一&#xff0c;有嵌套查询&#xff0c;并且子查询中用了union all合并两个查询时&#xff0c;前一个查询用了order by&#xff0c;那么会报错并提示ORA-00907:missing right parenthesis缺少右括号&#xff1a; select * from ( select t.* from emp t where t.jobMANAGER ord…

mysql重复记录大于十的数据库_面试官:在使用mysql数据库时,遇到重复数据怎么处理?...

前言前段时间&#xff0c;很多人问我能不能写一些数据库的文章&#xff0c;正好自己在测试mysql数据库性能的时候&#xff0c;出现了一个问题&#xff0c;也就是出现了很多重复的数据&#xff0c;想起来自己long long ago写过一篇类似的&#xff0c;仅此就拿来总结了一下。如果…

线程组的概念

一&#xff0c;线程组和线程的结构&#xff1a;树形结构 每个Thread必然存在于一个ThreadGroup中&#xff0c;Thread不能独立于ThreadGroup存在。 执行main()方法线程的名字是main 如果在new Thread时没有显式指定&#xff0c;那么默认将父线程&#xff08;当前执行new Threa…

mysql中ak替换键_数据库:唯一性约束_alternate key(替换键) mySQL Oracle 数据库 ak 唯一性约束...

数据库:唯一性约束_alternate key(替换键) mySQL Oracle 数据库 ak 唯一性约束数据库:唯一性约束所谓唯一性约束(unique constraint)不过是数据表内替代键的另一个名称而已。替代键(alternate key)可以是数据表内不作为主键的其他任何列&#xff0c;只要该键对该数据表唯一即可…

Oracle自定义类型

Oracle自定义类型可以通过type/create type来声明或者创建 一&#xff0c;四种创建方式 1.1&#xff0c;使用create type创建object类型 create or replace type obj_type as object(id number,name varchar2(50 byte),birthday date); 1.2&#xff0c;使用create type创建…

Oracle/mysql查询语句的执行过程

执行顺序 from on join/pivot/unpivot(mysql没有pivot和unpivot) where group by having select distinct order by limit&#xff08;oralce没有&#xff09; 书写顺序 select distinct <select_list> from <left_table> <join_type>join <righ…

mysql定时sql脚本_定时执行的SQL脚本

因为要同步一个表&#xff0c;所以每天要同步一次数据&#xff0c;但是对SQL不是精通的我&#xff0c;为了测试写了一段代码来测试定时功能创建一个存储过程&#xff0c;是用来插数据的&#xff0c;没有输出和输出参数create or replace procedure temp_pro asbegininsert into…

mysql xml语句_Mysql语句

xml文件转义字符处理(1)(2)直接写转义后的字符1、mysql里批量修改表内某个字段内的部分数据UPDATE inventory_stockSET batchno REPLACE(batchno,-20-201901,-50-2019)2、ON DUPLICATE KEY UPDATE根据主键判断是新增还是修改(也可以有两个或多个主键)INSERT INTO TABLE (a,c) …

destoon网站mysql分表_destoon : 常用数据库操作

destoon在初始化系统后系统会自动连接数据库&#xff0c;并将数据库操作对象保存在$db。对于数据库操作方法参考include/db_mysql.class.php函数原型&#xff0c;我来写几个常用数据库操作。1、读取单条信息$S $db->get_one("SELECT * FROM {$DT_PRE}table WHERE xxxy…

delphi7 mysql控件_Delphi7连接MySql数据库-DBGrid控件显示数据

一个简单的Delphi7小程序&#xff0c;使用MySql数据库做简单查询&#xff0c;用DBGrid控件显示结果&#xff0c;实现过程如下&#xff1a;(1)在MySql中新建demouser表&#xff0c;插入记录用于测试。(2)在Delphi7中新建项目。(3)在From中添加组件。组件Panel&#xff1a;pnl1组…