我正在使用gm,尝试根据图像大小处理图像。由于 "size" getter 需要回调函数,所以我不能在以下行中使用 size。
我想要做的就是:
function processImage(url) {
var img = gm(this.getImgStream(url));
var width, height;
img.size(function(err, val) {
width = val.width;
height = val.height;
});
// I want to use width here, but it's undefined when this line is executed.
if (width > 500) width = 500;
return img.resize(width)
}
我希望在以下的调整大小方法中使用宽度。有没有办法同步获取尺寸或等待回调完成?只要可能,我不想使用实例变量。