我正在使用节点模块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,因此从磁盘读取不是一个真正的选择。
答案 0 :(得分:0)
我找到了答案,那是RTFM:http://nodejs.org/api/fs.html
fs.readFileSync(filename,[options])# fs.readFile的同步版本。返回文件名的内容。
如果指定了编码选项,则此函数返回一个字符串。否则返回缓冲区。
我的代码是将编码设置为'二进制'它返回一个字符串,而不是一个缓冲区。