PHP判断一个gif图片是否为动态图片的方法,
本文实例讲述了PHP判断一个gif图片是否为动态图片的方法。分享给大家供大家参考。具体方法如下:
如何使用PHP来判断一个gif图片是否为动态图片(动画)?首先想到的是使用getimagesize()函数来看type值,发现都是gif,所以这个办法是不可行的。下面是作者在网上看到的一个函数,用来判断gif是否为动图的。贴出来和大家分享
例子如下:
复制代码 代码如下:
/*
* 判断图片是否为动态图片(动画)
*/
function isAnimatedGif($filename) {
$fp=fopen($filename,'rb');
$filecontent=fread($fp,filesize($filename));
fclose($fp);
return strpos($filecontent,chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0')===FALSE?0:1;
}
或者这样做:
用PHP判断一个gif图片是不是动画(多帧)
复制代码 代码如下:
function IsAnimatedGif($filename)
{
$fp = fopen($filename, 'rb');
$filecontent = fread($fp, filesize($filename));
fclose($fp);
return strpos($filecontent,chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0') === FALSE?0:1;
}
echo IsAnimatedGif("51windows.gif");
?>
例子2
gif动画是gif89格式的,发现文件开头是gif89。但是很多透明图片也是用的gif89格式,
GOOGLE到的:可以检查文件中是否包含:chr(0×21).chr(0xff).chr(0×0b).'NETSCAPE2.0'
chr(0×21).chr(0xff) 是gif图片中扩展功能段的标头,'NETSCAPE2.0'是扩展功能执行的程序名
程序代码如下:
复制代码 代码如下:
function check($image){
$content= file_get_contents($image);
if(preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$content)){
return true;
}else{
return false;
}
}
if(check('/home/lyy/luoyinyou/2.gif')){
echo'真是动画';
}else{
echo'不是动画';
}
?>
测试发现,读取1024字节足够了,因为此时读取的数据流中正好包含了 chr(0×21).chr(0xff).chr(0×0b).'NETSCAPE2.0'
希望本文所述对大家的PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool