Guest User

Untitled

a guest
Jan 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. const getPixels = require('get-pixels')
  2. const GIFEncoder = require('gif-encoder')
  3. const fs = require('fs')
  4. const imagemin = require('imagemin')
  5. const imageminGiflossy = require('imagemin-giflossy')
  6. const imageminGifsicle = require('imagemin-gifsicle')
  7.  
  8. const images = [
  9. {delay: 200, path: 'image/1.png'},
  10. {delay: 200, path: 'image/1.png'},
  11. {delay: 200, path: 'image/1.png'},
  12. {delay: 200, path: 'image/1.png'},
  13. {delay: 200, path: 'image/1.png'},
  14. ]
  15. const images1 = [
  16. {delay: 200, path: 'image/2.jpg'},
  17. {delay: 200, path: 'image/2.jpg'},
  18. {delay: 200, path: 'image/2.jpg'},
  19. {delay: 200, path: 'image/2.jpg'},
  20. {delay: 200, path: 'image/2.jpg'},
  21. ]
  22. const images2 = [
  23. {delay: 200, path: '1.png'},
  24. {delay: 200, path: '1.png'},
  25. {delay: 200, path: '1.png'},
  26. {delay: 200, path: '1.png'},
  27. {delay: 200, path: '1.png'},
  28. ]
  29.  
  30. const images3 = [
  31. {delay: 200, path: '2.jpg'},
  32. {delay: 200, path: '2.jpg'},
  33. {delay: 200, path: '2.jpg'},
  34. {delay: 200, path: '2.jpg'},
  35. {delay: 200, path: '2.jpg'},
  36. ]
  37.  
  38.  
  39. function wrapGif(imgData, pathReadyGif = '1.gif') {
  40. const gif = new GIFEncoder(240, 400)
  41. const stream = fs.createWriteStream(pathReadyGif)
  42. let counter = 0
  43.  
  44. gif.pipe(stream)
  45. gif.setQuality(20)
  46. gif.setDelay(100)
  47. gif.writeHeader()
  48. gif.setRepeat(0)
  49.  
  50. return new Promise((res, rej) => {
  51. function addToGif() {
  52. getPixels(imgData[counter].path, (error, pixels) => {
  53. if (error) {
  54. return rej()
  55. }
  56. gif.setDelay(imgData[counter].delay)
  57. gif.addFrame(pixels.data)
  58. gif.read()
  59. if (counter === imgData.length - 1) {
  60. gif.finish()
  61. return res()
  62. }
  63. counter++
  64. addToGif()
  65. })
  66. }
  67. addToGif()
  68. })
  69. }
  70.  
  71. (async () => {
  72. try {
  73. await imagemin([`${__dirname}/2.gif`, `${__dirname}/1.gif`], `${__dirname}/build/`, {
  74. plugins: [
  75. imageminGifsicle({
  76. colors: 100,
  77. optimizationLevel: 3,
  78. }),
  79. ]
  80. })
  81. console.log('ok')
  82. }
  83. catch (error) {
  84. console.log(error)
  85. }
  86. })()
  87.  
  88. // wrapGif(images2)
Add Comment
Please, Sign In to add comment