Skip to content

Instantly share code, notes, and snippets.

@marinhoarthur
Last active April 25, 2017 20:34
Show Gist options
  • Save marinhoarthur/778fb37722c29fff4b54a38ceee3dc18 to your computer and use it in GitHub Desktop.
Save marinhoarthur/778fb37722c29fff4b54a38ceee3dc18 to your computer and use it in GitHub Desktop.
js code to retrieve the format of any given image (square, horizontal, vertical) *jquery free*
function getAspectRatio(img) {
return img.width / img.height;
}
function getImgFormat(img) {
var aspectRatio = getAspectRatio(img);
if(aspectRatio === 1) {
return 'square';
} else if (aspectRatio > 1) {
return 'horizontal';
} else {
return 'vertical';
}
}
// usage
var myImg = document.getElementById('myImg');
getImgFormat(myImg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment