Home > Backend Development > PHP Tutorial > php判断GIF图片是否为动画的方法_PHP

php判断GIF图片是否为动画的方法_PHP

WBOY
Release: 2016-06-01 11:52:00
Original
934 people have browsed it

本文介绍了PHP判断GIF图片是动画的方法,具体步骤如下:

首先,gif动画是gif89格式的,发现文件开头是gif89。但是很多透明图片也是用的gif89格式,

GOOGLE到的:可以检查文件中是否包含:chr(0×21).chr(0xff).chr(0×0b).'NETSCAPE2.0'

chr(0×21).chr(0xff) 是gif图片中扩展功能段的标头,'NETSCAPE2.0'是扩展功能执行的程序名

程序代码如下:

<&#63;php
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'不是动画';
}
&#63;>

Copy after login

这段代码还是可以再优化的:

因为实际上 chr(0×21).chr(0xff).chr(0×0b).'NETSCAPE2.0' 只在文件头部出现,可以 echo 来看看 ,但不是最头部,是在头部的某一个位置,所以,严格来说需要读取一部分文件,但不用全部,这样可以加快速度和节省内存。

程序可改写如下:

function check_animation($image_file){
$fp = fopen($image_file, 'rb');
$image_head = fread($fp,1024);
fclose($fp);
return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head) &#63; true : false;
}

Copy after login

测试发现,读取1024字节足够了,因为此时读取的数据流中正好包含了 chr(0×21).chr(0xff).chr(0×0b).'NETSCAPE2.0'

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template