Since img.size()
is asynchronous, you can't do the operation synchronously (which means you also can't use return
for a return value). Therefore, you need img.size()
to finish before you can do anything else. You can either assign a callback within the operation, or pass callbacks around:
function processImage(url, callback) {
var img = gm(this.getImgStream(url));
var width, height;
img.size(function(err, val) {
width = val.width;
height = val.height;
callback(err, width, height);
});
};
processImage(url, function(err, width, height) {
if (width > 500) width = 500;
img.resize(width);
});