Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Each Image Natural Height With jQuery

Tags:

I have to read each images' natural height and I should make calculations. However I have some problem with read to natural height.

$('div.imgkirp img').each(function(){     console.log($(this).naturalHeight); }); 

It gets : (images number) undefined in console log. How can i read each of images' natural height ?

like image 850
Ayro Avatar asked Jan 26 '14 13:01

Ayro


People also ask

How to get image height in jQuery?

width(); alert(imageWidth); var imageHeight = Imgsize. height();

How to get height of image in JavaScript?

Answer: Use the JavaScript clientWidth property You can simply use the JavaScript clientWidth property to get the current width and height of an image. This property will round the value to an integer.

How to get image rendered size in JavaScript?

You can get the original width and height of an image using the HTML5 image naturalWidth and naturalHeight properties, which are supported in almost all major browsers. To get the current width and height, you can use the JavaScript clientWidth and clientHeight properties.

How to get uploaded image height and width in jQuery?

If you're using the jQuery library, you can programmatically load the image from the web and get its dimensions. The idea is to dynamically create an image element and use the . attr() method to set its src attribute to the image URL. Once the image is available, you can get its width and height properties.


2 Answers

Try to use property naturalHeight of image, using the prop method of the jQuery

$('div.imgkirp img').each(function(){     console.log($(this).prop('naturalHeight')); }); 
like image 58
Wallace Maxters Avatar answered Oct 14 '22 18:10

Wallace Maxters


var image = new Image(); image.src = $(this).attr("src"); alert('width: ' + image.naturalWidth + ' and height: ' + image.naturalHeight); 

This approach doesn't work in IE 8 and below versions, because it doesn't support 'naturalWidth' and 'naturalHeight' properties. To achieve the same use this code

var image = new Image(); image.src = $(this).attr("src"); image.onload = function() { console.log('height: ' + this.height); }; 
like image 36
Rohan Avatar answered Oct 14 '22 18:10

Rohan