Detect if a GIF is Animated

By  on  
WTF

If you tweet an animated GIF, you'll notice that Twitter takes it and converts it to an mp4.  I'd speculate that they do that conversion because an MP4 is better supported across platforms, at least in that they allow for playing/pause of the animation. And with a video you can prevent infinite looping, unlike a GIF.  How can you determine if a GIF is animated though?  I found a few good solutions.

Command Line with ImageMagick

ImageMagick, the amazing image manipulation library, provides a method for counting frames in a GIF:

./ImageMagick-6.9.0/bin/identify -format %n wtf.gif

The command above will provide you a count of the frames within the GIF.

animated-gif-detector

For those of you that prefer JavaScript (...my people...), I found a really simple animated GIF detection library on NPM:  animated-gif-detector.  Its API couldn't be shorter:

var fs = require('fs');
var animated = require('animated-gif-detector');

> animated(fs.readFileSync('wtf.gif'));
// true

> animated(fs.readFileSync('blank.gif'));
// false

The function call simply returns true or false, representing if the GIF is animated.  Exactly what I wanted!

As for what each library looks for, I've seen some ugly bit code I wont try to describe.  If you want to determine if a GIF is animated, however, it's as easy as the utilities above!

Recent Features

  • By
    9 More Mind-Blowing WebGL Demos

    With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities.  I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

Incredible Demos

  • By
    Facebook-Style Modal Box Using MooTools

    In my oh-so-humble opinion, Facebook's Modal box is the best modal box around. It's lightweight, subtle, and very stylish. I've taken Facebook's imagery and CSS and combined it with MooTools' awesome functionality to duplicate the effect. The Imagery Facebook uses a funky sprite for their modal...

  • By
    Prevent Page Zooming in Mobile Browsers

    Ever since I got my iPhone, I've been more agreeable in going places that my fiancee wants to go. It's not because I have any interest in checking out women's shoes, looking at flowers, or that type of stuff -- it's because my iPhone lets...

Discussion

  1. Only as a side note, Twitter is using mp4 because it’s an stream. Gif loading prevents the browser to fire the “ready” state until all of them are loaded. By using the video the event is fired meanwhile all the “gif” are being loaded in parallel.

  2. And of course there is this webservice: https://doesthisgifcontainananimation.com/

    • Christian Læirbag

      Hey Robert: Any Idea on how to put that service to work? I can’t find the correct syntax to use with http://i.imgur.com/tYqyhJT.gif for example. If you have the solution, please let me know. Ok?

    • It’s easy! In Javascript, you can call: encodeURIComponent("http://i.imgur.com/tYqyhJT.gif")

      Then you append the result to the domain, just like this:
      https://doesthisgifcontainananimation.com/http%3A%2F%2Fi.imgur.com%2FtYqyhJT.gif

      You can then see, whether the GIF contains an animation or not. :)

    • Christian Læirbag

      Oh, now I get it. I wasn’t understanding that encoding stuff. Thanks for clearing it out! :)

      By the way: I’m now trying to get result to be fetched from a Javascript or Yahoo Pipe, but I’m not getting close. If you have or manage to find something, please bring some words once more O.K.?

    • Christian Læirbag

      Duhh!… Forget about it. Yahoo Pipes is shutting down, so…Nevermind.

  3. Peter Galiba

    Please, don’t advocate fs.readFileSync as it is not a good approach in 99% of the cases. animated-gif-detector supports streams, so use them instead. And you don’t even have to continue reading the file when you have already detected that it is a animated gif. Animated gifs can be big, and reading big files into memory is not too efficient, especially if they are not even on your local machine, but read from a network.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!