Reputation: 2476
Using javascript is there any method to calculate the dimensions of an image just from knowing its filesize?
I am assuming that differen't formats (JPEG/PNG) require a different calculation but I am only looking to use JPEG or PNG.
Upvotes: 2
Views: 357
Reputation: 700342
Based on the file type, and also the nature of the images that you will be dealing with, you could calculate an approximate image size from an average bits/pixel value.
However, how good the approximation is depends on how varying the images are. JPEG images can be saved with different compression rate, so the file sizes can vary a lot.
Upvotes: 1
Reputation: 16297
Not really. Because you can have a very high quality Jpg with x dimensions but a low quality with the same dimensions. The filesize is going to vary dramatically.
Upvotes: 1
Reputation: 413717
No, there's no way to do that reliably. The size of the compressed image depends a lot on the actual nature of an image. A 1600x1200 white rectangle compresses to almost nothing, while a color photo of a circus would be much much larger.
Now, image formats may contain easy-to-read headers (or trailers or whatever) that tell you the image dimensions directly. (I suspect in fact that virtually all image formats have such metadata, because it seems like a practical necessity, but I'm not an expert so I'll hedge :-)
Upvotes: 4