index.js
895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import GIF from 'omggif';
import { GifUtil, GifFrame, BitmapImage, GifCodec } from 'gifwrap';
const MIME_TYPE = 'image/gif';
export default () => ({
mime: { [MIME_TYPE]: ['gif'] },
constants: {
MIME_GIF: MIME_TYPE
},
decoders: {
[MIME_TYPE]: data => {
const gifObj = new GIF.GifReader(data);
const gifData = Buffer.alloc(gifObj.width * gifObj.height * 4);
gifObj.decodeAndBlitFrameRGBA(0, gifData);
return {
data: gifData,
width: gifObj.width,
height: gifObj.height
};
}
},
encoders: {
[MIME_TYPE]: data => {
const bitmap = new BitmapImage(data.bitmap);
GifUtil.quantizeDekker(bitmap, 256);
const newFrame = new GifFrame(bitmap);
const gifCodec = new GifCodec();
return gifCodec.encodeGif([newFrame], {}).then(newGif => {
return newGif.buffer;
});
}
}
});