Home  >  Article  >  Backend Development  >  PHP method to determine whether a GIF image is animated, _PHP tutorial

PHP method to determine whether a GIF image is animated, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:17744browse

How to determine whether a GIF image is animated in php,

This article introduces how PHP determines whether a GIF image is an animation. The specific steps are as follows:

First of all, the gif animation is in gif89 format, and it is 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, and 'NETSCAPE2.0' is the name of the program for executing the extended function

The program code is as follows:

<&#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;>

This code can still be optimized:

Because actually chr(0×21).chr(0xff).chr(0×0b).'NETSCAPE2.0' only appears at the head of the file. You can echo it to see, but it is not at the top. At a certain position in the head, so strictly speaking, part of the file needs to be read, but not all of it, which can speed up and save memory.

The program can be rewritten as follows:

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

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'

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840747.htmlTechArticlephp method to determine whether a GIF image is an animation. This article introduces the method for PHP to determine whether a GIF image is an animation. The specific steps As follows: First, the gif animation is in gif89 format, and it is found that the beginning of the file is...
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