2

please tell me how to get image size of all images in the page using jquery or javascript

Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178

3 Answers3

4

The only size you can get is the visible size. clientWidth and clientHeight are two DOM properties which do this. Example:

var image = document.getElementById("id");
var width = image.clientWidth;
var height = image.clientHeight;

If you're using jQuery, you can simply use $.width and $.height:

var width = $("id").width();
var height = $("id").height();

So, to get the size of all images, loop them through:

$("img").each(function()
{
    console.log(this.width());
    console.log(this.height());
});

If you need the real size, please see this question Get the real width and height of an image with JavaScript? (in Safari/Chrome)

Community
  • 1
  • 1
alexn
  • 57,867
  • 14
  • 111
  • 145
  • I have a form to upload an image, is there any way to get image dimensions on the client side before I process it on server side. – SIFE Nov 14 '12 at 00:15
  • @SIFE Yes, it is possible in newer browsers. Please ask this in a new question. – alexn Nov 14 '12 at 06:18
  • See my [question](http://stackoverflow.com/questions/13390167/how-to-get-image-dimensions-on-the-client-side) pls. stackoverflow must star thinking on url shortening service. – SIFE Nov 15 '12 at 01:18
2
$('img').each(function() {
   $(this).width(); // current image's width
   $(this).height(); // current image's height
});
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
1

please tell me how to get image size of all images in the page using jquery or javascript

You can use width() and height() method of jQuery:

$('img').each(function(){
   alert('width=' + $(this).width() + '\nHeight=' + $(this).height());
});

More Info:

Sarfraz
  • 377,238
  • 77
  • 533
  • 578