views:

110

answers:

4

Hello,

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

Thanks Jean

+2  A: 

Has already been asked and answered - http://stackoverflow.com/questions/623172/how-to-get-image-size-height-width-using-javascript

var img = document.getElementById('imageid'); 
var width = img.clientWidth;
var height = img.clientHeight;
David Liddle
A: 

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

Sorantis
A: 

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
+2  A: 

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