How to get image size from url
I am trying to do below code to get image size in KB / MB / GB (not width and height) from live url. But it doesn't work.
var xhr = new XMLHttpRequest();
xhr.open('HEAD', 'http://www.2015autoblog.com/wp-content/uploads/2014/09/2015-Toyota-Land-Cruiser-Front.jpg', true);
debugger
alert(xhr.status);
if ( xhr.status == 200 ) {
alert('Size in bytes: ' + xhr.getResponseHeader('Content-Length'));
} else {
alert('ERROR');
}
+3
source to share
1 answer
Try using this code
var tmpImg = new Image();
tmpImg.src="https://www.google.com/intl/en_com/images/srpr/logo3w.png"; //or document.images[i].src;
$(tmpImg).one('load',function(){
orgWidth = tmpImg.width;
orgHeight = tmpImg.height;
alert(orgWidth+"x"+orgHeight);
});
The above code will get the image size.
File size: No, you cannot get the size of an image using jQuery or pure JavaScript. The only way is to get it from the server using ajax.
You can get the url of the image by submitting it to a service page or server and have the page or service display the size of the image.
+11
source to share