54

As part of a web app, once images have been downloaded and rendered on a web page, I need to determine an image's file size (kb) and resolution within the browser context (so I could, for example, display that info on the page. This needs to be done client-side, obviously. Must be able to be solved x-browser without an ActiveX control or Java applet (IE7+, FF3+, Safari 3+, IE6 nice to have), though it doesn't have to be the same solution per browser.

Ideally this would be done using system Javascript, but if I absolutely need a JQuery or similar library (or a tiny subset of it), that could be done.

1
  • Note that the first three answers are about the incorrect writeup I started with (where I said I was talking about image upload). My apologies to those folks.
    – scottru
    Aug 21, 2009 at 7:11

11 Answers 11

86

Edit:

To get the current in-browser pixel size of a DOM element (in your case IMG elements) excluding the border and margin, you can use the clientWidth and clientHeight properties.

var img = document.getElementById('imageId'); 

var width = img.clientWidth;
var height = img.clientHeight;

Now to get the file size, now I can only think about the fileSize property that Internet Explorer exposes for document and IMG elements...

Edit 2: Something comes to my mind...

To get the size of a file hosted on the server, you could simply make an HEAD HTTP Request using Ajax. This kind of request is used to obtain metainformation about the url implied by the request without transferring any content of it in the response.

At the end of the HTTP Request, we have access to the response HTTP Headers, including the Content-Length which represents the size of the file in bytes.

A basic example using raw XHR:

var xhr = new XMLHttpRequest();
xhr.open('HEAD', 'img/test.jpg', true);
xhr.onreadystatechange = function(){
  if ( xhr.readyState == 4 ) {
    if ( xhr.status == 200 ) {
      alert('Size in bytes: ' + xhr.getResponseHeader('Content-Length'));
    } else {
      alert('ERROR');
    }
  }
};
xhr.send(null);

Note: Keep in mind that when you do Ajax requests, you are restricted by the Same origin policy, which allows you to make requests only within the same domain.

Check a working proof of concept here.

Edit 3:

1.) About the Content-Length, I think that a size mismatch could happen for example if the server response is gzipped, you can do some tests to see if this happens on your server.

2.) For get the original dimensions of a image, you could create an IMG element programmatically, for example:

var img = document.createElement('img');

img.onload = function () { alert(img.width + ' x ' + img.height); };

img.src='http://sstatic.net/so/img/logo.png';
10
  • 1
    Note that content-length is >= the real file size since the data can be encoded for transport. Aug 21, 2009 at 8:12
  • 1
    Thanks. A few followups: 1) what kind of encoding would happen for jpgs that could increase the size? 2) If the image is resized in-browser, is there any way to get the original image dimensions?
    – scottru
    Aug 21, 2009 at 16:07
  • hm doesnt work for me, what am i doing wrong (it shows me 0 )? $("#temp_image").html('<img src="uploads/obr1.jpg" alt="" id="tmp" />'); var img = document.getElementById('tmp'); alert(img.clientWidth);
    – mm.
    Sep 29, 2009 at 8:04
  • In case anyone else is curious to try this out, chrome at the very least will throw a cross site scripting error on data urls (i.e. from a canvas.toDataURL() result ). It's apparently expected behaviour.
    – euxneks
    May 15, 2013 at 21:12
  • 3
    That's old, but for the edit 3, instead of creating a new img element, you can get directly the img's naturalWidth and naturalHeight properties.
    – Kaiido
    Jan 31, 2018 at 3:48
19

Check the uploaded image size using Javascript

<script type="text/javascript">
    function check(){
      var imgpath=document.getElementById('imgfile');
      if (!imgpath.value==""){
        var img=imgpath.files[0].size;
        var imgsize=img/1024; 
        alert(imgsize);
      }
    }
</script>

Html code

<form method="post" enctype="multipart/form-data" onsubmit="return check();">
<input type="file" name="imgfile" id="imgfile"><br><input type="submit">
</form>
2
  • 2
    i really like this hack :) but sadly you can't set the value of an file upload input element :( Aug 12, 2014 at 12:45
  • the browser is displaying the file upload filed like as button, Here we can browse and pick a file. So I didn't set value for file upload
    – Mohanrajan
    Jul 21, 2016 at 12:00
14

Getting the Original Dimensions of the Image

If you need to get the original image dimensions (not in the browser context), clientWidth and clientHeight properties do not work since they return incorrect values if the image is stretched/shrunk via css.

To get original image dimensions, use naturalHeight and naturalWidth properties.

var img = document.getElementById('imageId'); 

var width = img.naturalWidth;
var height = img.naturalHeight;

p.s. This does not answer the original question as the accepted answer does the job. This, instead, serves like addition to it.

13

How about this:

var imageUrl = 'https://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg';
var blob = null;
var xhr = new XMLHttpRequest(); 
xhr.open('GET', imageUrl, true); 
xhr.responseType = 'blob';
xhr.onload = function() 
{
    blob = xhr.response;
    console.log(blob, blob.size);
}
xhr.send();

http://qnimate.com/javascript-create-file-object-from-url/

due to Same Origin Policy, only work under same origin

1
  • this one works but unfortunately gives cors policy error
    – Mamadmti
    Sep 23, 2023 at 7:21
12

Regarding the width and height:

var img = document.getElementById('imageId'); 

var width = img.clientWidth;
var height = img.clientHeight;

Regarding the filesize you can use performance

var size = performance.getEntriesByName(url)[0];
console.log(size.transferSize); // or decodedBodySize might differ if compression is used on server side
7
  • This is very cool! I'm aware of the performance APIS but never realized you could get details per URL. Mar 23, 2022 at 22:10
  • In my cases it always returns 0.
    – Jared
    Apr 13, 2022 at 14:50
  • Performance API did not work for me in Angular 10 but the blob method stated by White Wang in the answer did the trick although it will be slower... May 3, 2022 at 15:35
  • This works great, but unfortunately doesn't seem to work in Safari (or IE) due to lack of transferSize support caniuse.com/?search=transfersize Aug 9, 2022 at 20:26
  • What is url? Purely the url of the image?
    – tom10271
    Feb 2, 2023 at 9:02
3

Service workers have access to header informations, including the Content-Length header.

Service workers are a bit complicated to understand, so I've built a small library called sw-get-headers.

Than you need to:

  1. subscribe to the library's response event
  2. identify the image's url among all the network requests
  3. here you go, you can read the Content-Length header!

Note that your website needs to be on HTTPS to use Service Workers, the browser needs to be compatible with Service Workers and the images must be on the same origin as your page.

2
  • This has absolutely nothing to do with the question.
    – DJSpud
    Feb 21 at 20:16
  • 1
    @DJSpud it does not retrieve the width and height, but it has to do with the size of the image in KB, as asked in the question. Feb 28 at 14:59
2

Most folks have answered how a downloaded image's dimensions can be known so I'll just try to answer other part of the question - knowing downloaded image's file-size.

You can do this using resource timing api. Very specifically transferSize, encodedBodySize and decodedBodySize properties can be used for the purpose.

Check out my answer here for code snippet and more information if you seek : JavaScript - Get size in bytes from HTML img src

2

You can use generic Image object to load source dynamically then measure it:

    const img = new Image();
    img.src = this.getUrlSource()
    img.onload = ({target}) =>{
      let width = target.width;
      let height = target.height;
    }
1
  • 1
    I was looking for a solution where the image is not in the DOM but rather in a base64 string and this worked nicely
    – J. Barca
    Apr 2, 2021 at 2:08
0

You can get the dimensions using getElement(...).width and ...height.

Since JavaScript can't access anything on the local disk for security reasons, you can't examine local files. This is also true for files in the browser's cache.

You really need a server which can process AJAX requests. On that server, install a service that downloads the image and saves the data stream in a dummy output which just counts the bytes. Note that you can't always rely on the Content-length header field since the image data might be encoded. Otherwise, it would be enough to send a HTTP HEAD request.

-4
var img = new Image();
img.src = sYourFilePath;
var iSize = img.fileSize;
2
-9

The only thing you can do is to upload the image to a server and check the image size and dimension using some server side language like C#.

Edit:

Your need can't be done using javascript only.

1
  • 3
    This is not true... You can set the file path in JS, turn it into an image element and return the width and height, offset or not. (The browser does a little work, but you can do it with purely JS in a browser) Apr 8, 2013 at 22:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.