Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using this library in the browser? #10

Open
magoz opened this issue May 5, 2020 · 2 comments
Open

Using this library in the browser? #10

magoz opened this issue May 5, 2020 · 2 comments

Comments

@magoz
Copy link

magoz commented May 5, 2020

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!

@Ironolife
Copy link

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);
  });

@FRSgit
Copy link

FRSgit commented May 17, 2024

For the future travellers:
I've created a package that's working both under Node & browser environments: https://www.npmjs.com/package/@frsource/is-animated

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants