Open Bitware32 opened 4 years ago
Probably not relevant anymore but here is what worked for me. However, some GIFs turns into a mess. Destroyed colors and play too fast. I don't know what's wrong with it.
import { BitmapImage, GifFrame, GifUtil, GifCodec } from 'gifwrap'
const resizeFrame = async (frame, width, height) => {
try {
const image = await GifUtil.copyAsJimp(Jimp, frame)
const resizedGif = await image.resize(width, height)
const bitmap = new BitmapImage(resizedGif.bitmap)
GifUtil.quantizeDekker(bitmap, 256) // reduce the colors added by Jimp back to 256
return new GifFrame(bitmap)
} catch (e) {
console.error("Resizing gifwrap frame failed")
console.error(e)
}
}
const resizeGif = async (file, width, height) => {
try {
const gif = await GifUtil.read(file.buffer)
const uresolvedFrames = gif.frames.map(async (frame) => {
const resizedFrame = await resizeFrame(frame, width, height)
return resizedFrame
})
const newFrames = await Promise.all(uresolvedFrames)
const codec = new GifCodec()
const buffer = (await codec.encodeGif(newFrames, { loops: 0 })).buffer
return buffer
} catch (e) {
console.error("Resizing gif failed")
console.error(e)
}
}
Here is a gif that does NOT work.
Turns into this:
Using this gif with the code below https://media1.giphy.com/media/HnKSuvC39SdOM/source.gif
I am getting this error on GifUtil.write():
Is this a bug or am I doing something wrong? I'm trying to downsize the gif to 160x160px.