is-animated
is-animated copied to clipboard
Using this library in the browser?
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!
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);
});