Home  >  Article  >  Backend Development  >  How to determine whether a gif image is a dynamic image in PHP_PHP Tutorial

How to determine whether a gif image is a dynamic image in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:17:06788browse

How does PHP determine whether a gif image is an animated image?

Example

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

/*

* Determine whether the picture is a dynamic picture (animation)
*/

function isAnimatedGif($filename) {
 代码如下  
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");
?>
$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 (multi-frame)
 代码如下  

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'不是动画';
}
?>

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:
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' http://www.bkjia.com/PHPjc/895096.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/895096.htmlTechArticleHow does PHP determine whether a gif image is a dynamic image? The example code is as follows /* * Determine whether the image is a dynamic image (animation) ) */ function isAnimatedGif($filename) { $fp=fopen($filenam...
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