0

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

Thanks Jean

X10nD
  • 20,888
  • 44
  • 108
  • 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
  • 14,915
  • 15
  • 59
  • 81
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
  • 86,568
  • 42
  • 148
  • 166
0

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

Sorantis
  • 14,120
  • 4
  • 30
  • 36
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
  • 34,957
  • 46
  • 126
  • 166