Skip to content

Instantly share code, notes, and snippets.

@hdogan
Last active March 10, 2017 07:54
Show Gist options
  • Save hdogan/7adb17c87f80eff0f6fc82130b59f98d to your computer and use it in GitHub Desktop.
Save hdogan/7adb17c87f80eff0f6fc82130b59f98d to your computer and use it in GitHub Desktop.
Detects animated GIF from given file pointer resource or filename.
<?php
/**
* Detects animated GIF from given file pointer resource or filename.
*
* @param resource|string $file File pointer resource or filename
* @return bool
*/
function is_animated_gif($file)
{
$fp = null;
if (is_string($file)) {
$fp = fopen($file, "rb");
} else {
$fp = $file;
/* Make sure that we are at the beginning of the file */
fseek($fp, 0);
}
if (fread($fp, 3) !== "GIF") {
fclose($fp);
return false;
}
$frames = 0;
while (!feof($fp) && $frames < 2) {
if (fread($fp, 1) === "\x00") {
/* Some of the animated GIFs do not contain graphic control extension (starts with 21 f9) */
if (fread($fp, 1) === "\x2c" || fread($fp, 2) === "\x21\xf9") {
$frames++;
}
}
}
fclose($fp);
return $frames > 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment