Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save .gif to local file system via Node

I am using Gifshot to generate an animation. I am successfully generating a "file". However, it seems like the encoding is off. The image that's gets generated is being generated via 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, animation.gif gets generated to my local file system (it generates a 108kb file). However, when I open it, the animation doesn't actually exist. I'm not sure what I'm doing wrong. I know that Gifshot returns a Base 64 image. I assumed that was the issue. So, I tried integrating the SO answer found here. However, that didn't work either. I'm not sure what I'm doing wrong. Any help is much appreciated.

Thanks!

like image 540
JQuery Mobile Avatar asked Oct 19 '22 22:10

JQuery Mobile


1 Answers

Try the module 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');
          }
        }
      );                            
    }
  }
);
like image 108
fregante Avatar answered Oct 21 '22 16:10

fregante