Created
April 10, 2016 09:31
-
-
Save hsleonis/73f5698d7a28e2db93e50fd1942bdee2 to your computer and use it in GitHub Desktop.
Image natural size
This file contains hidden or 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
| /** | |
| * Solution for unsupported browsers (create dummy image to get real dimensions): | |
| */ | |
| function realImgDimension(img) { | |
| var i = new Image(); | |
| i.src = img.src; | |
| return { | |
| naturalWidth: i.width, | |
| naturalHeight: i.height | |
| }; | |
| } | |
| var myImage = document.getElementById('myImage'); | |
| myImage.addEventListener('load', function() { | |
| var realSize = realImgDimension(this); | |
| console.log('My width is: ', realSize.naturalWidth); | |
| console.log('My height is: ', realSize.naturalHeight); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment