GraphicsMagick: toBuffer()流产生空缓冲区

时间:2022-05-03 09:00:48

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.

但是,这会产生一个错误:流产生空缓冲区。我一直在玩,使用imageMagick和graphicsMagick,仍然是一样的。

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

如果我代替toBuffer(…带写(asd.jpg,函数(err)……

it actually writes a proper file.

它实际上写了一个合适的文件。

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

在写这个问题的时候,我找到了解决方案,见回复

4 个解决方案

#1


10  

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

我遇到了同样的问题:安装graphicsmagick二进制文件。

sudo apt-get install graphicsmagick

sudo apt-get安装graphicsmagick

#2


9  

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

setFormat(jpg)引起的问题。将其更改为setFormat('jpeg')解决了它。

#3


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. 缺少安装:您没有正确安装graphicsmagick或imagemagick。确保你已经安装了两个。
  3. Invalid Parameter: remove all parameters until it works. jpg instead of jpeg is a common mistake
  4. 无效参数:删除所有参数,直到它生效。jpg而不是jpeg是一个常见的错误。
  5. Wrong library You made use of graphicmagick but wanted to use imagemagick? (e.g. for webp conversion). Add imageMagick configuration:
  6. 错误的库你使用了graphicmagick但是想要使用imagemagick?(例如webp转换)。添加imageMagick配置:

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

Feel free to add more ideas as comment.

请随意添加更多的想法作为评论。

#4


0  

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.

我也偶然发现了这个错误。对我来说,解决方案是将允许的内存从128Mb增加到256Mb。我注意到这个过程需要137Mb才能成功。

#1


10  

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

我遇到了同样的问题:安装graphicsmagick二进制文件。

sudo apt-get install graphicsmagick

sudo apt-get安装graphicsmagick

#2


9  

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

setFormat(jpg)引起的问题。将其更改为setFormat('jpeg')解决了它。

#3


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. 缺少安装:您没有正确安装graphicsmagick或imagemagick。确保你已经安装了两个。
  3. Invalid Parameter: remove all parameters until it works. jpg instead of jpeg is a common mistake
  4. 无效参数:删除所有参数,直到它生效。jpg而不是jpeg是一个常见的错误。
  5. Wrong library You made use of graphicmagick but wanted to use imagemagick? (e.g. for webp conversion). Add imageMagick configuration:
  6. 错误的库你使用了graphicmagick但是想要使用imagemagick?(例如webp转换)。添加imageMagick配置:

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

Feel free to add more ideas as comment.

请随意添加更多的想法作为评论。

#4


0  

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.

我也偶然发现了这个错误。对我来说,解决方案是将允许的内存从128Mb增加到256Mb。我注意到这个过程需要137Mb才能成功。