Home > php教程 > php手册 > PHP如何判断一个gif图片是否为动态图片

PHP如何判断一个gif图片是否为动态图片

WBOY
Release: 2016-06-13 09:23:49
Original
1158 people have browsed it

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;
}

或者这样做
用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'

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template