代码之家  ›  专栏  ›  技术社区  ›  doekman

是否可以在客户端检测gif动画图像?

  •  19
  • doekman  · 技术社区  · 16 年前

    是否可以在客户端检测gif动画图像?

    onload 事件,因为它将被解雇 every frame loaded

    2 回复  |  直到 16 年前
        1
  •  0
  •   Residuum    16 年前

    我刚写了一些检测动画GIF的JS。适用于除IE 9之外的大多数现代浏览器。

    免责声明: 只有当图像的域源与您加载脚本的页面相同时,这才有效。

    请参阅最新版本代码的要点: https://gist.github.com/3012623

    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 4-byte sequence (\x00\x21\xF9\x04)
            // * 4 variable bytes
            // * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)
            // We read through the file til we reach the end of the file, or we've found 
            // at least 2 frame headers
            for (i=0, len = length - 9; i < len, frames < 2; ++i) {
                if (arr[i] === 0x00 && arr[i+1] === 0x21 &&
                    arr[i+2] === 0xF9 && arr[i+3] === 0x04 &&
                    arr[i+8] === 0x00 && 
                    (arr[i+9] === 0x2C || arr[i+9] === 0x21))
                {
                    frames++;
                }
            }
    
            // if frame count > 1, it's animated
            cb(frames > 1);
        });
        request.send();
    }
    
        2
  •  14
  •   Will    12 年前

    我不知道在客户端有什么办法,但我不确定。您可以做的是在服务器端解析HTML代码和任何引用的gif,并为这些图像添加一个类。但这并不是真正推荐的,因为它至少涉及一次额外的HTML解析+对每个gif的解析。如您所见 this example in PHP ,从CPU负载的角度来看,检查GIF也并非易事。