var i = new Image();
i.onload = function(){
alert( i.width+", "+i.height );
};
i.src = imageData;
对于同步使用,只需将其包装成如下promise:
function getImageDimensions(file) {
return new Promise (function (resolved, rejected) {
var i = new Image()
i.onload = function(){
resolved({w: i.width, h: i.height})
};
i.src = file
})
}
然后,您可以使用wait以同步编码方式获取数据:
var dimensions = await getImageDimensions(file)
我发现使用。自然宽度
和。自然重量
的效果最好。
const img = new Image();
img.src = 'https://via.placeholder.com/350x150';
img.onload = function() {
const imgWidth = img.naturalWidth;
const imgHeight = img.naturalHeight;
console.log('imgWidth: ', imgWidth);
console.log('imgHeight: ', imgHeight);
};