Skip to content

Instantly share code, notes, and snippets.

@marckubischta
Forked from lakenen/detectanimation.js
Last active June 20, 2020 07:49
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marckubischta/261ad8427a214022890b to your computer and use it in GitHub Desktop.
Save marckubischta/261ad8427a214022890b to your computer and use it in GitHub Desktop.
function isAnimatedGif(src, cb) {
var request = new XMLHttpRequest();
request.open('GET', src, true);
request.responseType = 'arraybuffer';
request.addEventListener('load', function () {
var arr = new Uint8Array(request.response),
i, len, length = arr.length, frames = 0;
// make sure it's a gif (GIF8)
if (arr[0] !== 0x47 || arr[1] !== 0x49 ||
arr[2] !== 0x46 || arr[3] !== 0x38)
{
cb(false);
return;
}
//ported from php http://www.php.net/manual/en/function.imagecreatefromgif.php#104473
//an animated gif contains multiple "frames", with each frame having a
//header made up of:
// * a static 3-byte sequence (\x00\x21\xF9
// * one byte indicating the length of the header (usually \x04)
// * variable length header (usually 4 bytes)
// * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)
// We read through the file as long as we haven't reached the end of the file
// and we haven't yet found at least 2 frame headers
for (i=0, len = length - 3; i < len && frames < 2; ++i) {
if (arr[i] === 0x00 && arr[i+1] === 0x21 && arr[i+2] === 0xF9)
{
blocklength = arr[i+3];
afterblock = i + 4 + blocklength;
if (afterblock + 1 < length &&
arr[afterblock] === 0x00 &&
(arr[afterblock+1] === 0x2C || arr[afterblock+1] === 0x21))
{
frames++;
}
}
}
// if frame count > 1, it's animated
cb(frames > 1);
});
request.send();
}
@Juraj-Masiar
Copy link

Missing variable declarations for blocklength and afterblock?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment