Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image size from URL [duplicate]

Tags:

jquery

I am trying below code to get the image size in KB/MB/GB (not width and height) from live URL. But it's not working.

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');
}
like image 283
Prashant Sarvaiya Avatar asked Oct 17 '14 07:10

Prashant Sarvaiya


1 Answers

Try this code it is working

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 gets the image dimension.

For the file size: No, there is no way you can get the image size using jQuery or pure JavaScript. The only way is to get it from server side using ajax.

You can get the image url send it to a service or server side page and have the page or service return the image size.

like image 74
user46487 Avatar answered Nov 15 '22 08:11

user46487