Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

anyway to get image file size using javascript(or jquery like) [duplicate]

like:

<img src="http://www.google.com/intl/en_ALL/images/srpr/logo1w.png">

i want get logo1w.png file size 7.3kb

how to implement it?

like image 852
Koerr Avatar asked Jul 17 '10 17:07

Koerr


4 Answers

Check out this : Determining image file size + dimensions via Javascript?

like image 55
bernedef Avatar answered Sep 22 '22 12:09

bernedef


You could do an HTTP HEAD request for the image URL. This will return the HTTP headers, which include the content-length (a.k.a. file size). This does require an additional request to the server, which is not very efficient when the image is generated instead of being served statically.

like image 40
Manfre Avatar answered Nov 12 '22 03:11

Manfre


I don't know of any single way to consistently get the exact file size, but if you're willing to jump through some hoops, you could:

1) Estimate based on the dimensions and the file type. Because of compression techniques there's some wide variation, but you could probably run some statistics and come up with a half-decent heuristic.

2) In IE, use the fileSize property of an image. In other browsers that support Canvas, use the technique described in this question on getting image data in JavaScript to grab the dataURL, then do the math (multiply by 3/4, since base64 is a 4/3 size increase).

Edit

3) The suggestion others have given (particularly as elaborated in the question linked in Fabien Bernede's answer) about getting HTTP Headers is brilliant AND precise. Probably runs afoul of cross-domain restrictions in some cases, but still probably the most consistent and effective method.

like image 3
Weston C Avatar answered Nov 12 '22 03:11

Weston C


You cannot in javascript (sorry even magical jQuery cannot help here!). You would need a server side script to fetch the image for you and calculate the size which you could return to your page.

Note - downvoters beware, the OP does not own the google domain!

like image 1
redsquare Avatar answered Nov 12 '22 05:11

redsquare