0

I want to get the image size using javascript or jquery. something similar to imagesize().

Thanks Jean

X10nD
  • 21,638
  • 45
  • 111
  • 152

4 Answers4

2

Has already been asked and answered - How to get image size (height & width) using JavaScript?

var img = document.getElementById('imageid'); 
var width = img.clientWidth;
var height = img.clientHeight;
Community
  • 1
  • 1
David
  • 15,150
  • 15
  • 61
  • 83
2

That depends. If the image already has width/height styles/attributes applied to it, you will not get the image's width/height, but you will get the resized dimensions. Here's a possible solution:

function getImageSize(img) {
    var clone = img.clone();
    clone.width("auto")
         .height("auto")
         .css("position", "absolute")
         .css("left", -9999)
         .css("top", -9999);
    $(document.body).append(clone);
    var width = clone.width();
    var height = clone.height();
    clone.remove();
    return [width, height];
}

Untested, but it should work. However, if you're sure the image is not resized from css/html, there's no need for this complication and you can use what the others have said.

Felix
  • 88,392
  • 43
  • 149
  • 167
0

var width = $("#img").width();

Sorantis
  • 14,496
  • 5
  • 31
  • 37
0

you can use .attr('width') and .attr('height') unless you mean size in bytes?

this wouldnt work if the img is being rescaled with css

Haroldo
  • 36,607
  • 46
  • 127
  • 169