获取图片的宽高
function get_image_dom_natural_wh(img_dom) {
return new Promise((resolve) => {
if (img_dom.naturalWidth && img_dom.naturalHeight) {
return resolve({ width: img_dom.naturalWidth, height: img_dom.naturalHeight });
} else {
const img = new Image();
img.src = img_dom.src;
img.onload = () => {
return resolve({ width: img.width, height: img.height });
};
img.onerror = () => {
return resolve({ width: 0, height: 0 });
};
}
});
}
naturalWidth和naturalHeight是html5新增的属性,它们可以直接获取图片的原始宽高
。而且这在Fixefox/Chrome/Safari/Opera/IE9里已经实现
。
js 获取图片的宽高