开发者

Calculate the dimensions of an image by its filesize

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 (开发者_如何学PythonJPEG/PNG) require a different calculation but I am only looking to use JPEG or PNG.


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 :-)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜