Home >Backend Development >PHP Tutorial >PHP method to determine whether a gif image is a dynamic image, _PHP tutorial

PHP method to determine whether a gif image is a dynamic image, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:43826browse

PHP method to determine whether a gif image is a dynamic image,

The example in this article describes how PHP determines whether a gif image is a dynamic image. Share it with everyone for your reference. The specific method is as follows:

How to use PHP to determine whether a gif image is a dynamic image (animation)? The first thing that comes to mind is to use the getimagesize() function to look at the type value and find that they are all gif, so this method is not feasible. Below is a function that the author saw on the Internet, which is used to determine whether a gif is an animated picture. Post it and share it with everyone

Examples are as follows:

Copy code The code is as follows:
/*
* Determine whether the picture is a dynamic picture (animation)
*/
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;
}

Or do this:
Use PHP to determine whether a gif image is animated (multiple frames)
Copy code The code is as follows:
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");
?>

Example 2
The gif animation is in gif89 format, and it was found that the beginning of the file is gif89. But many transparent pictures also use the gif89 format,
GOOGLE: You can check whether the file contains: chr(0×21).chr(0xff).chr(0×0b).'NETSCAPE2.0'
chr(0×21).chr(0xff) is the header of the extended function segment in the gif image, 'NETSCAPE2.0' is the program name for the extended function execution
The program code is as follows:
Copy code The code is as follows:
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'It's really animated';
}else{
echo'Not an animation';
}
?>

The test found that reading 1024 bytes is enough, because the data stream read at this time exactly contains chr(0×21).chr(0xff).chr(0×0b).'NETSCAPE2.0'

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914052.htmlTechArticlePHP’s method of determining whether a gif image is a dynamic image. This article describes an example of how PHP determines whether a gif image is dynamic. Picture method. Share it with everyone for your reference. The specific methods are as follows...
Statement:
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