【发布时间】:2015-03-09 14:40:32
【问题描述】:
我正在寻找一种工作方式来使用 CollectionFS transformWrite 函数中的 GM 方法,具体取决于图像大小。在 GM 中实现了一个 size 方法,但这是异步工作的,因此似乎无法使用。
我尝试了以下方法:
gm(readStream, fileObj.name()).size(function(err, dimensions){
if (err) {
console.log('err with getting size:');
console.log(err);
}
console.log('Result of media_size:');
console.log(dimensions);
// here do smth depends on the dimensions ...
gm(readStream, fileObj.name()).resize('1200', '630').stream().pipe(writeStream);
});
当我在 CollectionFS 函数中使用上述 sn-p 时,我收到此错误:
错误:gm().stream() 或 gm().write() 带有不可读的流。
这似乎是我使用异步函数的问题 - 删除异步函数时,上传工作完美,但我无法访问上传图像的尺寸。
只有访问fileObj、readStream 和writeStream 时,是否有解决方案以同步方式获取图像的尺寸?
编辑:
感谢 Jasper 对 wrapAsync 的提示。我对其进行了测试并使用了此代码:
var imgsize;
var img = gm(readStream, fileObj.name());
imgsize = Meteor.wrapAsync(img.size, img);
console.log('call wrapAsync:');
var result;
try {
result = imgsize();
} catch (e) {
console.log('Error:');
console.log(e)
}
console.log('((after imgsize()))');
查看 console.logs 时,脚本在“调用 wrapAsync”后停止 - 也没有返回错误,因此很难判断问题所在。我还尝试使用带有Meteor.wrapAsync(imagesize); 和imgsize(readStream) 的NPM 包“imagesize”,这会导致相同的结果:“call wrapAsync:”之后没有控制台日志。
【问题讨论】:
标签: node.js meteor graphicsmagick