views:

2547

answers:

7

(EDITED changes in italics) Goal: 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.

Thanks!

+1  A: 

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.

Aaron Digulla
+8  A: 

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.src='http://sstatic.net/so/img/logo.png';

img.onload = function () { alert(img.width + ' x ' + img.height); };
CMS
Note that content-length is >= the real file size since the data can be encoded for transport.
Aaron Digulla
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
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
A: 

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.

rahul
A: 

Only way is though Flash app. Since 99% of PCs (mac, linux, win) have flash installed, it's allmost like JS :) Simple tutorial: http://blog.flexexamples.com/2008/08/25/previewing-an-image-before-uploading-it-using-the-filereference-class-in-flash-player-10/

Hrvoje
A: 

You can find dimension of an image on the page using something like

document.getElementById('someImage').width

file size, however, you will have to use something server-side

cobbal
A: 

Yes, you can work out the file size with javascript only, By using the following API: http://www.filemd5.net/api and checking the size property of the response. The API support JSONP which enables you to make the call cross domain without any issues.

JohnnieWalker
A: 

var img=new Image(); img.src=sYourFilePath; var iSize=img.fileSize;

Brett