is-animated icon indicating copy to clipboard operation
is-animated copied to clipboard

Using this library in the browser?

Open magoz opened this issue 4 years ago • 2 comments

Hi, I'm working on a project and I'm trying to determine within the browser if a user-uploaded image via file input is animated. Does this library work in the browser?

I've tried passing an ArrayBuffer to isAnimate() but it doesn't work. I've also tried encoding the ArrayBuffer with btoa() and passing it to isAnimate() but it doesn't work either.

Thanks!

magoz avatar May 05 '20 14:05 magoz

I seem to get it working by converting ArrayBuffer to node.js Buffer: https://nodejs.org/api/buffer.html

you may need a polyfill: https://www.npmjs.com/package/buffer

isAnimated.ts

// @ts-ignore
import isBufferAnimated from 'is-animated';

export const isAnimated = async (file: File) =>
  new Promise<boolean>((res, rej) => {
    const fileReader = new FileReader();

    fileReader.onload = (ev) => {
      // Convert to Buffer
      const buffer = Buffer.from(fileReader.result as ArrayBuffer);

      const result = isBufferAnimated(buffer) as boolean;

      return res(result);
    };

    fileReader.onerror = (err) => rej(err);

    fileReader.readAsArrayBuffer(file);
  });

Ironolife avatar Apr 05 '22 11:04 Ironolife