Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get original dimensions of resized html image element

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)?

like image 362
devios1 Avatar asked Mar 15 '12 17:03

devios1


1 Answers

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

like image 135
antyrat Avatar answered Sep 25 '22 01:09

antyrat