Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created April 10, 2016 09:31
Show Gist options
  • Select an option

  • Save hsleonis/73f5698d7a28e2db93e50fd1942bdee2 to your computer and use it in GitHub Desktop.

Select an option

Save hsleonis/73f5698d7a28e2db93e50fd1942bdee2 to your computer and use it in GitHub Desktop.
Image natural size
/**
* 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