我正在尝试通过gm
in进行一些图像处理collectionFS
,因为我需要读取一个流并将其写回同一个文件,我必须使用一个临时文件 - 如下所示。
我想检查图像是否大于 1000px。在这种情况下,它应该重新调整为 1000 像素。
不幸的是,这不起作用,因为我得到了错误TypeError: Object [object Object] has no method 'pipe'
和Error: gm().stream() or gm().write() with a non-readable stream.
var fs = Npm.require('fs'),
file = Images.findOne({ _id: fileId }),
read = file.createReadStream('public'),
filename = '/tmp/gm_' + Date.now(),
temp = fs.createWriteStream(filename);
if (method == 'resize') {
// resize to 1000px, if image is bigger
gmread = gm(read);
gmread.size(function(err, size){
if(size.width > 1000) {
gmread.resize('1000').stream();
}
});
}
gmread.on('end', Meteor.bindEnvironment(function (error, result) {
if (error) console.warn(error);
var tmpread = fs.createReadStream(filename);
write = file.createWriteStream('public');
tmpread.on('end', Meteor.bindEnvironment(function (error, result) {
if (error) console.warn(error);
}));
tmpread.pipe(write);
}));
gmread.pipe(temp);