Achu
Achu

Reputation: 93

How to get Byte size of a image file using node.js

I need to get the size of a image files which is in a web page, using node.js .For this I used some node modules, but it gives size of all other resources like js file, html size etc.So how to get the size of a request(request for a image file) ? . How to find the load time of a image file ?

Upvotes: 4

Views: 3536

Answers (2)

Sebastian Stumpf
Sebastian Stumpf

Reputation: 2791

I assume you that you are trying to get the file size of an image via http and not the images dimensions. (Please correct me if I am mistaken)

You could simply send a HEAD request to the resource and have a look at the Content-Length header:

var http = require('http');

var req = http.request({ 
    host: 'www.gravatar.com',
    port: 80,
    path: '/avatar/b9da20552a71c4d3c4c6dc2d55418cf2',
    method: 'HEAD' }, 
    function(res) {
        console.log("Size: " + res.headers['content-length']);
    }
).end();

Upvotes: 5

Kinz
Kinz

Reputation: 310

It seems this post was helpful to another user, concerning the method of grabbing a file's bytesize. You can probably ignore the parts about getting the dimensions.

Determining image file size + dimensions via Javascript?

As for the other problem, the finding loadtime, this might work:

var obj = new Image();
obj.src = "imglocation.png";
for (i = 1; i != -1; i++) {
     if (obj.complete) { /*do stuff*/ } }

This might work. I'm not a JavaScripter so I'm not attuned highly attuned to it. And though this isn't Node.js, I'm sure you can alter the code to use it easily?

Upvotes: 0

Related Questions