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