以获取百度logo为例。
function getImageRect(src) {
return new Promise((resolve, reject) => {
const image = new Image();
image.src = src;
if (image.complete) {
resolve({
width: image.width,
height: image.height
});
} else {
image.onload = function () {
resolve({
width: image.width,
height: image.height
});
}
}
image.onerror = err => reject(err);
});
}
var imgSrc = 'https://www.baidu.com/img/bd_logo1.png?where=super';
getImageRect(imgSrc).then(({ width, height }) => {
console.log({ width, height }); // {width: 540, height: 258}
});
网友评论