Php determines whether a GIF image is animated,
This article describes how PHP judges GIF images as animations. The specific steps are as follows:
First, the GIF animation is in the gif89 format and the file starts with gif89. However, many transparent images are also in the gif89 format,
GOOGLE: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 program name for executing the extended function.
The program code is as follows:
<? Phpfunction 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 animate';} else {echo 'not animate';}?>
This code can be further optimized:
Because actually chr (0 × 21 ). chr (0xff ). chr (0 × 0b ). 'netscape2. 0' only appears in the file header. You can check it with echo, but it is not the most header. It is in a certain position in the header. Therefore, you must read some files, but not all of them, this 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;}
Test found that,It is enough to read 1024 bytes, because the data stream read now contains chr (0 × 21). chr (0xff). chr (0 × 0b). 'netscape2. 0'