1

Possible Duplicate:
Get real image width and height with JavaScript in Safari/Chrome?

Is there a way to know the true width and height of an image using jQuery when the image is scaled down in css - without any extra data of dimensions - any link or code example would be helpful thanks

Community
  • 1
  • 1
ONYX
  • 5,679
  • 15
  • 83
  • 146
  • Possible duplicate: **[Get real image width and height with JavaScript in Safari/Chrome?](http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome)** and **[Get image width and height](http://stackoverflow.com/questions/799710/get-image-width-and-height)** – Siva Charan Dec 05 '12 at 18:07

2 Answers2

17

Try using naturalWidth and naturalHeight.

var domElement = $('yourImage')[0]; // or document.getElementById('yourImageId');
domElement.naturalWidth
domElement.naturalHeight
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    If that works, then I think it's a better solution than the one suggested in [that other question](http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome) – GolezTrol Dec 05 '12 at 18:08
3

Try something like this:

$("<img/>").attr("src", imgSrc).load(function() {
        pic_real_width = this.width;   
        pic_real_height = this.height;          
    });

The thing is, you can only access image true width and height when the image is loaded, u could also load the image without any container to some place where you can not see it and ask for ("img").css('width'), but i think this is a more elegant way to do it. Hope this is helpful.

joao
  • 334
  • 2
  • 9