4

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 ?

2 Answers 2

5

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.