Created
January 12, 2018 19:44
-
-
Save dmitryshelomanov/1a5c9746f26dfd76de5d18340925c968 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getPixels = require('get-pixels') | |
const GIFEncoder = require('gif-encoder') | |
const fs = require('fs') | |
const imagemin = require('imagemin') | |
const imageminGiflossy = require('imagemin-giflossy') | |
const imageminGifsicle = require('imagemin-gifsicle') | |
const images = [ | |
{delay: 200, path: 'image/1.png'}, | |
{delay: 200, path: 'image/1.png'}, | |
{delay: 200, path: 'image/1.png'}, | |
{delay: 200, path: 'image/1.png'}, | |
{delay: 200, path: 'image/1.png'}, | |
] | |
const images1 = [ | |
{delay: 200, path: 'image/2.jpg'}, | |
{delay: 200, path: 'image/2.jpg'}, | |
{delay: 200, path: 'image/2.jpg'}, | |
{delay: 200, path: 'image/2.jpg'}, | |
{delay: 200, path: 'image/2.jpg'}, | |
] | |
const images2 = [ | |
{delay: 200, path: '1.png'}, | |
{delay: 200, path: '1.png'}, | |
{delay: 200, path: '1.png'}, | |
{delay: 200, path: '1.png'}, | |
{delay: 200, path: '1.png'}, | |
] | |
const images3 = [ | |
{delay: 200, path: '2.jpg'}, | |
{delay: 200, path: '2.jpg'}, | |
{delay: 200, path: '2.jpg'}, | |
{delay: 200, path: '2.jpg'}, | |
{delay: 200, path: '2.jpg'}, | |
] | |
function wrapGif(imgData, pathReadyGif = '1.gif') { | |
const gif = new GIFEncoder(240, 400) | |
const stream = fs.createWriteStream(pathReadyGif) | |
let counter = 0 | |
gif.pipe(stream) | |
gif.setQuality(20) | |
gif.setDelay(100) | |
gif.writeHeader() | |
gif.setRepeat(0) | |
return new Promise((res, rej) => { | |
function addToGif() { | |
getPixels(imgData[counter].path, (error, pixels) => { | |
if (error) { | |
return rej() | |
} | |
gif.setDelay(imgData[counter].delay) | |
gif.addFrame(pixels.data) | |
gif.read() | |
if (counter === imgData.length - 1) { | |
gif.finish() | |
return res() | |
} | |
counter++ | |
addToGif() | |
}) | |
} | |
addToGif() | |
}) | |
} | |
(async () => { | |
try { | |
await imagemin([`${__dirname}/2.gif`, `${__dirname}/1.gif`], `${__dirname}/build/`, { | |
plugins: [ | |
imageminGifsicle({ | |
colors: 100, | |
optimizationLevel: 3, | |
}), | |
] | |
}) | |
console.log('ok') | |
} | |
catch (error) { | |
console.log(error) | |
} | |
})() | |
// wrapGif(images2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment