As mentioned in the comments, this has already been answered here, although in pure JavaScript not JQuery. I have adapted that answer to:
- Use JQuery
- Work with a static image
- Created a simple function that you can call
Here's the function...
function getImageSize(img, callback){
img = $(img);
var wait = setInterval(function(){
var w = img.width(),
h = img.height();
if(w && h){
done(w, h);
}
}, 0);
var onLoad;
img.on('load', onLoad = function(){
done(img.width(), img.height());
});
var isDone = false;
function done(){
if(isDone){
return;
}
isDone = true;
clearInterval(wait);
img.off('load', onLoad);
callback.apply(this, arguments);
}
}
You can call the function with an image element, and a callback accepting width and height arguments...
getImageSize($('#imageHero'), function(width, height){
$('#imageDiv').height(height);
});
Fiddle - To see the full effect make sure the image is not in your cache (append a random to the image source).