This is a function for manipulating some images via graphicsmagick
in my meteor application.
这是一个在我的流星应用程序中通过graphicsmagick处理一些图像的功能。
If the image is being cropped, I want to check if its width is >900px. Then it should be resized to 900px.
如果正在裁剪图像,我想检查其宽度是否> 900px。然后应将其调整为900px。
This complete function is not working, as the callback of gmread.size()
has to be finished before doing the return gmread.stream
- which is currently not the case. But I don't know how to get this synchron/asynchron thing working...
这个完整的函数不起作用,因为gmread.size()的回调必须在返回gmread.stream之前完成 - 目前不是这种情况。但我不知道如何让这个同步/异步的东西工作......
function imageManipulation(inputId, method) {
var inStream = Files.findOneStream({ _id: inputId }),
gmread;
// do some image manipulation depending on given `method`
if (method == 'crop') {
gmread = gm(inStream);
// check if image width > 900px, then do resize
gmread.size(function(err, size) {
if (size.width > 900) {
gmread = gmread.resize('900');
}
});
}
return gmread.stream('jpg', Meteor.bindEnvironment(function(err, stdout, stderr) {
stderr.pipe(process.stderr);
if (!err) {
var outStream = Files.upsertStream({ _id: outputFileId }, {}, function(err, file) {
if (err) { console.warn("" + err); }
return;
});
return stdout.pipe(outStream);
}
}));
});
Update
I tried to use meteorhacks:async
, but I do get the error Exception in callback of async function: TypeError: Object [object Object] has no method 'size'
.
我试图使用meteorhacks:async,但我确实在异步函数的回调中得到错误Exception:TypeError:Object [object Object]没有方法'size'。
function imageManipulation(inputId, method) {
var inStream = Files.findOneStream({ _id: inputId }),
gmread;
if (method == 'crop') {
gmread = gm(inStream);
// Asnyc doesn't work
gmread = Async.runSync(function(done) {
gmread.size(function(err, size) {
if (size.width > 900) {
gmread = gmread.resize('900');
}
done(null, gmread);
});
});
}
return gmread.stream('jpg', Meteor.bindEnvironment(function(err, stdout, stderr) {
stderr.pipe(process.stderr);
if (!err) {
var outStream = Files.upsertStream({ _id: outputFileId }, {}, function(err, file) {
if (err) { console.warn("" + err); }
return;
});
return stdout.pipe(outStream);
}
}));
});
1 个解决方案
#1
0
you can use meteorhacks:async
that pause the execution until you invoke done() callback as shown below.
你可以使用meteorhacks:async,暂停执行直到你调用done()回调,如下所示。
function imageManipulation(inputId, method) {
var inStream = Files.findOneStream({ _id: inputId }),
gmread;
// do some image manipulation depending on given `method`
if (method == 'crop') {
gmread = gm(inStream);
// check if image width > 900px, then do resize
gmread.size(function(err, size) {
if (size.width > 900) {
gmread = gmread.resize('900');
}
});
}
var response = Async.runSync(function(done) {
gmread.stream('jpg', Meteor.bindEnvironment(function(err, stdout, stderr) {
stderr.pipe(process.stderr);
if (!err) {
var outStream = Files.upsertStream({ _id: outputFileId }, {}, function(err, file) {
if (err) { console.warn("" + err); }
return;
});
done(null, stdout.pipe(outStream));
}
}));
});
return response.result
});
#1
0
you can use meteorhacks:async
that pause the execution until you invoke done() callback as shown below.
你可以使用meteorhacks:async,暂停执行直到你调用done()回调,如下所示。
function imageManipulation(inputId, method) {
var inStream = Files.findOneStream({ _id: inputId }),
gmread;
// do some image manipulation depending on given `method`
if (method == 'crop') {
gmread = gm(inStream);
// check if image width > 900px, then do resize
gmread.size(function(err, size) {
if (size.width > 900) {
gmread = gmread.resize('900');
}
});
}
var response = Async.runSync(function(done) {
gmread.stream('jpg', Meteor.bindEnvironment(function(err, stdout, stderr) {
stderr.pipe(process.stderr);
if (!err) {
var outStream = Files.upsertStream({ _id: outputFileId }, {}, function(err, file) {
if (err) { console.warn("" + err); }
return;
});
done(null, stdout.pipe(outStream));
}
}));
});
return response.result
});