PHP method for judging whether an gif picture is a dynamic picture

  • 2021-08-05 09:05:39
  • OfStack

In this paper, the method of judging whether an gif picture is a dynamic picture by PHP is described with an example. Share it for your reference. The specific methods are as follows:

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

Examples are as follows:

/*
 * Judge whether the picture is a dynamic picture or not ( 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 judge whether an gif picture is animation (multi-frame)
<?php
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 is found that the file begins with gif89. But many transparent pictures are also in gif89 format.
GOOGLE to: You can check whether the file contains: chr (0 × 21). chr (0xff). chr (0 × 0b). 'NETSCAPE 2.0'
chr (0 × 21). chr (0xff) is the header of the extended function section in the gif picture, and 'NETSCAPE2.0' is the program name of the extended function execution
The program code is as follows:
<?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' Really animated ';
}else{
echo' Not animation ';
}
?>

The test found that 1024 bytes was enough to read, because the read data stream just contained chr (0 × 21). chr (0xff). chr (0 × 0b). 'NETSCAPE 2.0'

I hope this article is helpful to everyone's PHP programming.


Related articles: