根据文件大小计算图像的尺寸
使用javascript,有没有什么方法可以通过知道图像的文件大小来计算图像的尺寸?
我假设不同的格式(JPEG/PNG)需要不同的计算,但我只想使用 JPEG 或 PNG。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,没有办法可靠地做到这一点。压缩图像的大小很大程度上取决于图像的实际性质。 1600x1200 的白色矩形几乎不会被压缩,而马戏团的彩色照片会大得多。
现在,图像格式可能包含易于阅读的标题(或预告片或其他内容),可以直接告诉您图像尺寸。 (事实上,我怀疑几乎所有图像格式都有这样的元数据,因为这似乎是实际需要,但我不是专家,所以我会回避:-)
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 :-)
并不真地。因为您可以拥有 x 尺寸的高质量 Jpg,但具有相同尺寸的低质量 Jpg。文件大小将会发生巨大变化。
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.
根据文件类型以及您将要处理的图像的性质,您可以根据平均位/像素值计算近似图像大小。
然而,近似值的好坏取决于图像的变化程度。 JPEG 图像可以使用不同的压缩率保存,因此文件大小可能会有很大差异。
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.