Skip to content

Instantly share code, notes, and snippets.

@simbathesailor
Created June 16, 2021 09:28
Show Gist options
  • Save simbathesailor/f0cab691ea4e9e20135c7ebb3b78d8c2 to your computer and use it in GitHub Desktop.
Save simbathesailor/f0cab691ea4e9e20135c7ebb3b78d8c2 to your computer and use it in GitHub Desktop.
getImageDimension.js
function getImageDimension(imgUrl) {
return new Promise((resolve, reject) => {
if (!imgUrl) {
reject({
status: "FAILURE"
});
}
try {
const img = new Image();
img.src = imgUrl;
img.style.visibility.hidden = true;
document.body.appendChild(img);
img.onload = (e) => {
const { width, height } = img.getBoundingClientRect(img);
resolve({
width,
height,
status: "SUCCESS"
});
img.remove();
};
img.onerror = () => {
resolve({
status: "FAILURE"
});
};
} catch (e) {
reject({
status: "FAILURE"
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment