Save .gif to local file system via Node

I use Gifshot to create animations. I successfully create a "file". However, the encoding seems to be disabled. The generated image is generated using the following code:

var images = [
  'http://i.imgur.com/2OO33vX.jpg',
  'http://i.imgur.com/qOwVaSN.png',
  'http://i.imgur.com/Vo5mFZJ.gif'
];

var gifshot = require('gifshot');
gifshot.createGIF(
  { 'images':images, 'numFrames': images.length }, 
  function(obj) {
    if (!obj.error) {
      fs.writeFile(
        './animation.gif', obj.image, 'base64', 
        function(err) {
          if (err) {
            alert(err);
          } else {
            alert('Should be all good');
          }
        }
      );                            
    }
  }
);

When the above code runs, the .gif animation is generated in my local file system (it generates a 108KB file). However, when I open it, the animation does not actually exist. I'm not sure what I'm doing wrong. I know Gifshot is returning a Base 64 image. I assumed this is a problem. So, I tried to integrate the SO answer found here . However, this did not work either. I'm not sure what I'm doing wrong. Any help is greatly appreciated.

Thanks!

+4
1

base64image-to-file

var images = [
  'http://i.imgur.com/2OO33vX.jpg',
  'http://i.imgur.com/qOwVaSN.png',
  'http://i.imgur.com/Vo5mFZJ.gif'
];

var gifshot = require('gifshot');
var base64ImageToFile = require('base64image-to-file'); //// new
gifshot.createGIF(
  { 'images':images, 'numFrames': images.length }, 
  function(obj) {
    if (!obj.error) {
      base64ImageToFile(obj.image, '.', 'animation.gif', //// new
        function(err) {
          if (err) {
            alert(err);
          } else {
            alert('Should be all good');
          }
        }
      );                            
    }
  }
);
+1

Source: https://habr.com/ru/post/1616077/


All Articles