Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Created November 15, 2022 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhaim1/e5a314ce2afc529cb6fcf096d7d40105 to your computer and use it in GitHub Desktop.
Save danielhaim1/e5a314ce2afc529cb6fcf096d7d40105 to your computer and use it in GitHub Desktop.
Return the natural dimensions of an image.
/**
* Return the natural dimensions of an image.
* @string img - The image to get the dimensions of.
* @return {object} - The natural width of the image.
* @return {null} - If the image has no natural dimensions.
*/
function getNaturalDimensions({ naturalWidth, naturalHeight }) {
// get the image format from the src attribute.
let width = naturalWidth;
let height = naturalHeight;
if (width && height) {
// return the natural width and height of the image.
return { width, height };
} else {
// return null if the image has no natural dimensions.
return { width: null, height: null };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment