Last active
April 25, 2017 20:34
-
-
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*
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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