PHP determines whether a GIF image is a dynamic image,

Source: Internet
Author: User

PHP determines whether a GIF image is a dynamic image,

This example describes how to use PHP to determine whether a GIF image is a dynamic image. Share it with you for your reference. The specific method is as follows:

How can I use PHP to determine whether a GIF image is a dynamic image (animation )? The first thought was to use the getimagesize () function to view the type value. It was found that all the values were gif, so this method is not feasible. The following is a function that the author sees on the Internet to determine whether a gif is an animation. Post it to share with you

Example:
Copy codeThe Code is as follows :/*
* Determines whether an image is a dynamic image (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? ;
}
Or do this:
Use PHP to determine whether a GIF image is animated (multiple frames)
Copy codeThe Code is as follows: <? 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? ;
}
Echo IsAnimatedGif ("51windows.gif ");
?>
Example 2
GIF animation is in gif89 format. It is found that 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:
Copy codeThe 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 'real animate ';
} Else {
Echo 'not animate ';
}
?>
The test found that reading 1024 bytes is sufficient because the data stream read at this time contains chr (0 × 21 ). chr (0xff ). chr (0 × 0b ). 'netscape2. 0'

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.