Is there a simple and efficient way to get the true dimensions (in JavaScript) of an image that is displayed in an <img>
element with a potentially different rendered size (e.g. via max-height
or max-width
)?
There is present naturalWidth
and naturalHeight
DOM attributes.
For example:
var image = new Image();
image.src = "test.jpg";
image.onload = function() {
alert('width - ' + image.naturalWidth);
alert('height - ' + image.naturalHeight);
}
Or see example on jsFiddle.
More info at MDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With