Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphicsMagick: toBuffer() Stream yields empty buffer

I am trying to read a file into a buffer, resize it and then write it to disk using the following example code:

function processImage(data) {
gm(data, 'test.jpg')
    .resize('300x300')
  .background('white')
  .flatten()
  .setFormat('jpg')
  .toBuffer(function(err, buffer) {
    if (err) {
        throw err;
    } else {
        fs.writeFile('asd.jpg', buffer);
    }
  });
}

However, this generates an error Error: Stream yields empty buffer. I have played around, used imageMagick and graphicsMagick, still the same.

If i substitute
toBuffer(...
with
write('asd.jpg', function(err) ...

it actually writes a proper file.

EDIT While writing this question I found the solution, see reply

like image 559
Bernhardt Scherer Avatar asked Feb 16 '16 12:02

Bernhardt Scherer


3 Answers

Was facing the same problem, in my case: install the graphicsmagick binary.

sudo apt-get install graphicsmagick

like image 129
Edo Avatar answered Oct 15 '22 13:10

Edo


setFormat('jpg')
caused the problems. Changing it to
setFormat('jpeg') solved it.

like image 9
Bernhardt Scherer Avatar answered Oct 15 '22 14:10

Bernhardt Scherer


Stream yields empty buffer

This indicates a general error of graphiksmagick. Here are some probable causes:

  1. Missing Installation: you did not install graphicsmagick or imagemagick correctly. Make sure you installed both and
  2. Invalid Parameter: remove all parameters until it works. jpg instead of jpeg is a common mistake
  3. Wrong library You made use of graphicmagick but wanted to use imagemagick? (e.g. for webp conversion). Add imageMagick configuration:

const gm = require('gm').subClass({imageMagick: true});

Feel free to add more ideas as comment.

like image 8
Simon Fakir Avatar answered Oct 15 '22 14:10

Simon Fakir