Created
January 11, 2021 15:34
-
-
Save gaffling/eaf6dbcea454a035335c9b4d75335de8 to your computer and use it in GitHub Desktop.
[is animated GIF] Detect if a GIF file is animated or not #php #function #gif
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* ----------------------------------------------------------------------------- */ | |
/* [is animated GIF] Detect if a GIF file is animated or not #php #function #gif */ | |
/* ----------------------------------------------------------------------------- */ | |
/** | |
* Detect if a GIF file is animated or not | |
* | |
* We read through the file til we reach the end of the file, or we've found at least 2 frame headers | |
* an animated gif contains multiple "frames", with each frame having a header made up of: | |
* a static 4-byte sequence (\x00\x21\xF9\x04) | |
* 4 variable bytes | |
* a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?) | |
* @see https://php.net/manual/en/function.imagecreatefromgif.php#104473 | |
*/ | |
function isAnimatedGIF($gif) { | |
if ( !($fh=@fopen($gif, 'rb'))) return false; | |
$count = 0; | |
while ( !feof($fh) and $count < 2 ) { | |
$chunk = fread($fh, 1024 * 100); // read 100kb at a time | |
$count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches); | |
} | |
fclose($fh); | |
return $count > 1; | |
} | |
echo 'GIF but not animated ' . isAnimatedGIF('error.gif') . ' ""=false<br><br>'; | |
echo 'This GIF is animated ' . isAnimatedGIF('https://talks.php.net/presentations/slides/intro/test.gif').' "1"=true'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment