Determine original size of image cross browser?

JavascriptImage

Javascript Problem Overview


Is there a reliable, framework independent way of determining the physical dimensions of a <img src='xyz.jpg'> resized on the client side?

Javascript Solutions


Solution 1 - Javascript

You have 2 options:

Option 1:

Remove the width and height attributes and read offsetWidth and offsetHeight

Option 2:

Create a JavaScript Image object, set the src, and read the width and height (you don't even have to add it to the page to do this).

function getImgSize(imgSrc) {
    var newImg = new Image();

    newImg.onload = function() {
      var height = newImg.height;
      var width = newImg.width;
      alert ('The image size is '+width+'*'+height);
    }

    newImg.src = imgSrc; // this must be done AFTER setting onload
}

Edit by Pekka: As agreed in the comments, I changed the function to run on the ´onload´ event of the image. Otherwise, with big images, height and width would not return anything because the image was not loaded yet.

Solution 2 - Javascript

Images (on Firefox at least) have a naturalWidth/height property so you can use img.naturalWidth to get the original width

var img = document.getElementsByTagName("img")[0];
img.onload=function(){
    console.log("Width",img.naturalWidth);
    console.log("Height",img.naturalHeight);
}

Source

Solution 3 - Javascript

You can preload the image into a javascript Image object, then check the width and height properties on that object.

Solution 4 - Javascript

/* Function to return the DOM object's in crossbrowser style */
function widthCrossBrowser(element) {
	/* element - DOM element */
	
	/* For FireFox & IE */
	if(		element.width != undefined && element.width != '' && element.width != 0){
		this.width	=	element.width;
	}
	/* For FireFox & IE */
	else if(element.clientWidth != undefined && element.clientWidth != '' && element.clientWidth != 0){
		this.width	=	element.clientWidth;
	}
	/* For Chrome * FireFox */
	else if(element.naturalWidth != undefined && element.naturalWidth != '' && element.naturalWidth != 0){
		this.width	=	element.naturalWidth;
	}
	/* For FireFox & IE */
	else if(element.offsetWidth != undefined && element.offsetWidth != '' && element.offsetWidth != 0){
		this.width	=	element.offsetWidth;
	}		
		/*
			console.info(' widthWidth width:',		element.width);
			console.info(' clntWidth clientWidth:',	element.clientWidth);
			console.info(' natWidth naturalWidth:',	element.naturalWidth);
			console.info(' offstWidth offsetWidth:',element.offsetWidth);		
			console.info(' parseInt(this.width):',parseInt(this.width));
		*/
	return parseInt(this.width);
	
}	

var elementWidth	= widthCrossBrowser(element);

Solution 5 - Javascript

Just changing a little bit Gabriel's second option, to be more easy to use:

function getImgSize(imgSrc, callback) {
    var newImg = new Image();

    newImg.onload = function () {
        if (callback != undefined)
            callback({width: newImg.width, height: newImg.height})
    }

    newImg.src = imgSrc;
}

Html:

<img id="_temp_circlePic" src="http://localhost/myimage.png" 
style="width: 100%; height:100%">

Sample call:

getImgSize($("#_temp_circlePic").attr("src"), function (imgSize) {
	// do what you want with the image's size.
	var ratio = imgSize.height / $("#_temp_circlePic").height();
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPekkaView Question on Stackoverflow
Solution 1 - JavascriptGabriel McAdamsView Answer on Stackoverflow
Solution 2 - JavascriptBugsterView Answer on Stackoverflow
Solution 3 - JavascriptMylesView Answer on Stackoverflow
Solution 4 - JavascriptRomanView Answer on Stackoverflow
Solution 5 - JavascriptWagner Bertolini JuniorView Answer on Stackoverflow