在imagecreatefromgif()函数的PHP手册页中有一个简短的代码片段,应该是你所需要的:

由ZeBadger提供的图片评论#59787

在search同样问题的解决scheme时,我注意到php.net网站对Davide和Kris所指代码的后续操作,但根据作者的说法,内存密集程度更低,可能更less的磁盘密集。

我会在这里复制它,因为它可能是有趣的。

来源: http : //www.php.net/manual/en/function.imagecreatefromgif.php#88005

function is_ani($filename) { if(!($fh = @fopen($filename, 'rb'))) return false; $count = 0; //an animated gif contains multiple "frames", with each frame having a //header made up of: // * a static 4-byte sequence (\x00\x21\xF9\x04) // * 4 variable bytes // * a static 2-byte sequence (\x00\x2C) // We read through the file til we reach the end of the file, or we've found // at least 2 frame headers while(!feof($fh) && $count < 2) { $chunk = fread($fh, 1024 * 100); //read 100kb at a time $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', $chunk, $matches); } fclose($fh); return $count > 1; }

这是工作function:

/** * Thanks to ZeBadger for original example, and Davide Gualano for pointing me to it * Original at http://it.php.net/manual/en/function.imagecreatefromgif.php#59787 **/ function is_animated_gif( $filename ) { $raw = file_get_contents( $filename ); $offset = 0; $frames = 0; while ($frames < 2) { $where1 = strpos($raw, "\x00\x21\xF9\x04", $offset); if ( $where1 === false ) { break; } else { $offset = $where1 + 1; $where2 = strpos( $raw, "\x00\x2C", $offset ); if ( $where2 === false ) { break; } else { if ( $where1 + 8 == $where2 ) { $frames ++; } $offset = $where2 + 1; } } } return $frames > 1; }

如果给定的文件太大,用file_get_contents读取整个文件可能会花费太多的内存。 我已经重新考虑了以前给出的函数,它只读取了足够的字节来检查帧,并且一find至less2帧就返回。

<?php /** * Detects animated GIF from given file pointer resource or filename. * * @param resource|string $file File pointer resource or filename * @return bool */ function is_animated_gif($file) { $fp = null; if (is_string($file)) { $fp = fopen($file, "rb"); } else { $fp = $file; /* Make sure that we are at the beginning of the file */ fseek($fp, 0); } if (fread($fp, 3) !== "GIF") { fclose($fp); return false; } $frames = 0; while (!feof($fp) && $frames < 2) { if (fread($fp, 1) === "\x00") { /* Some of the animated GIFs do not contain graphic control extension (starts with 21 f9) */ if (fread($fp, 1) === "\x2c" || fread($fp, 2) === "\x21\xf9") { $frames++; } } } fclose($fp); return $frames > 1; }

animationGIF必须具有以下string

"\x21\xFF\x0B\x4E\x45\x54\x53\x43\x41\x50\x45\x32\x2E\x30"

php gd gif动画,我可以检测使用PHP和GD的animationGIF?相关推荐

  1. php gd support not found.,安装php71w-gd并获取错误GD库扩展不适用于此PHP安装

    我在centos 7服务器和nginx web服务器上使用laravel web框架,我安装了php71w-gd,当我想上传文件时仍然出现此错误 Intervention \ Image \ Exce ...

  2. docker php安装gd扩展_给docker里的php安装gd扩展

    docker官方镜像为安装php扩展封装了函数,为开发者提供了很大的便利,以下以Dockerfile的形式演示安装gd扩展的方法,安装gd扩展需要安装几个依赖包,安装依赖包使用系统命令,安装命令根据基 ...

  3. php源码 gd,CentOS7(lnmp环境)php源码编译安装gd库

    源码编译安装php5.6的时候,为投省事,只安装了mbstring和php-fpm扩展,其他的都没有–enable进去.不过如果所有的扩展都enale进去的话,运行configure脚本的时候会崩溃掉 ...

  4. gd mysql_简单的图形计数器需要MYSQL,GD的支持_MySQL

    /*** hit_count表只一个字段hit_count page_visit_record表是用来控制一个IP一天内只产生一个计数器跳动 CREATE TABLE page_visit_recor ...

  5. feachall php_timthumb.php详解

    //定义版本信息 define ('VERSION', '2.8.10'); //如果有配置文件,则加载timthumb-config.php,没有的话使用下面的值 if( file_exists(d ...

  6. php 压缩gif 不动,调整GIF动画文件的大小而不会破坏动画

    如果您具有imagemagick访问权限,则可以执行以下操作: system("convert big.gif -coalesce coalesce.gif"); system(& ...

  7. centos php 开启libgdgd_Linux下开启PHP GD库支持

    在linux系统中开启GD库支持,有以下几种方法. 1,检测GD库是否安装命令 代码示例: php5 -m | grep -i gd 或者 php -i | grep -i --color gd 2, ...

  8. 上传到服务器gd不支持,安装dedecms出现GD不支持。我的php5.5的。怎么解决?

    在php.ini中开启GD即可. Windows下开启PHP的GD库支持 找到php.ini,打开内容,找到: ;extension=php_gd2.dll 把最前面的分号";"去 ...

  9. php 配置 gd2,配置PHP对gd库的支持

    搭建zabbix的时候遇到有对PHP的需求检测,发现没有对gd的支持,记录下... GD库是php处理图形的扩展库,它提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片,也可以给图 ...

最新文章

  1. C++智能指针:unique_ptr详解
  2. OPENCV图像创建,保存和复制
  3. shell注释与赋值
  4. Java当中的运算符
  5. Microsoft SQL server 2000 安装挂起 mssql 2000 挂起无法安装的问题
  6. C++调用Matlab 注意事项
  7. python怎么打开一个窗口_python – 使按钮一次只打开一个窗口(通过关闭Toplevel窗口启用按钮)...
  8. 华为云联合浙江大学构建新冠科研开放知识图谱
  9. java代码隐藏面消除算法,java常面的几种排序算法
  10. SDL_BlitSurface的参数是两个PNG时,如何保护其透明度
  11. GRE零基础50-60天出分随感--V159 Q170
  12. jquery easyui后台模板
  13. AndroidStudio之Git提交代码出现author ‘xxx‘ is not ‘Name ‘ and matches no existing author 的解决方法
  14. SIRIUS更新日志|SIRIUS人脸识别更新日志|天狼星人脸识别更新日志
  15. uml 菱形_UML图符号的含义
  16. 【ATE-SENT协议】使用LabVIEW采集并解析SENT协议
  17. Linux下文件的备份
  18. msdn和系统下载位置
  19. 这才是纯爷们!!!~~~~~~
  20. 网龙3D人物部件制作工艺介绍

热门文章

  1. Android 颜色渲染(九) PorterDuff及Xfermode详解
  2. SharePoint 2013:解决添加域名后每次都需要登录的问题
  3. WCF 第八章 安全 客户端认证
  4. Hibernate 补充 ManyToOne、OneToMany、OneToOne的使用例
  5. Android 设备启动时,APP应用自启动
  6. 修改表字段属性_使用postman修改SAP Marketing Cloud contact主数据
  7. VS2010 C++下编译调试MongoDB源码 r2.2.2
  8. Python四大金刚之三:元组
  9. java layoutmanager_Java Swing 探索(一)LayoutManager
  10. java jsp ajax_ajax的json传值方式在jsp页面中的应用