GraphicsMagick: toBuffer() Stream yields empty buffer

别等时光非礼了梦想. 提交于 2019-12-09 07:58:55

问题


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


回答1:


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

sudo apt-get install graphicsmagick




回答2:


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




回答3:


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.




回答4:


I also stumbled upon this error. For me the solution was to increase the allowed memory from 128Mb to 256Mb. I noticed the process needed 137Mb in order to succeed.



来源:https://stackoverflow.com/questions/35433249/graphicsmagick-tobuffer-stream-yields-empty-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!