【问题标题】:graphicsmagick fails to get size when using gm(buffer,file)使用 gm(buffer,file) 时 graphicsmagick 无法获取大小
【发布时间】:2014-10-16 04:19:11
【问题描述】:

我正在使用节点模块 gm,并且正在尝试识别 PNG 图像的大小。我制作了以下小程序来说明我的问题。如果您使用带文件名的 gm 构造函数,则会检索大小。如果您传递缓冲区 + 文件名提示,它将失败。

返回的错误是:

can't buffer image and identify size { [Error: Command failed: gm identify: No decode delegate for this image format ().
gm identify: Request did not return an image.
] code: 1, signal: null }

代码示例:

var fs = require('fs');
var gm = require('gm');

var buffer = fs.readFileSync('./test/test_image.png',{encoding:'binary'});

gm('./test/test_image.png').size(function(err,size) {
    if (err) console.log("can't identify from disk read");
    else {
        gm(buffer,'test_image.png').size(function(err,size) {
            if (err) console.log("can't buffer image and identify size");
            else console.log("all good");
            process.exit(0);
        }); 
    }   
});

关于如何解决这个问题的任何想法?我的真实用例是从 web POST 端点获取 base64 编码的图像数据,因此从磁盘读取并不是一个真正的选择。

【问题讨论】:

    标签: node.js graphicsmagick


    【解决方案1】:

    我找到了答案,是 RTFM:http://nodejs.org/api/fs.html

    fs.readFileSync(filename, [options])# fs.readFile 的同步版本。返回文件名的内容。

    如果指定了编码选项,则此函数返回一个字符串。否则返回一个缓冲区。

    我的代码将编码设置为“二进制”,它返回一个字符串,而不是缓冲区。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多