如何使用图像的宽度和高度来估计图像的文件大小?
我最近使用 php 编写了一个图像调整大小程序,该程序的工作原理是从另一台服务器下载图像,调整它们的大小并将它们保存到我们自己的服务器上。
坏消息是我的托管帐户只允许 64M 的 php 内存限制,而且这并不是为了调整我的客户端上传的 HUUUUGE 文件大小(3 - 4mb)而设置的。如果它遇到这些图像并破坏脚本,它会发出致命错误。
尽管我已通知该客户此缺陷,但该客户仍在继续上传大图像,并且脚本不断损坏。
我可以在使用 getimagesize()
下载图像之前获取图像的宽度和高度,如果我可以使用此信息来计算出总文件大小,我可以在图像调整器启动之前突破并用一个不错的“无可用图像”替代方案补充图像。
假设位深度为 24,如何使用图像文件的宽度和高度准确估计其大小?
I have recently written an image resizing program using php, which works by downloading images off another server, resizing them and saving them to our own.
The bad news is that my Hosting account only allows a php memory limit of 64M, and this is just not set up to resize the HUUUUGE file sizes that my client is uploading (3 - 4mb). It spits out a fatal error if it meets these images and breaks the script.
Even though I have notified said client of this drawback, said client continues to upload large images and script keeps breaking.
I can obtain the width and the height of the image before downloading it using getimagesize()
, and if I could use this info to work out the total file size I could break out before the image resizer gets going and suppliment the image with a nice "no image available" alternative.
How can I make an accurate estimation of an images file size using its width and height, assuming it has a bit depth of 24?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
内存中的图像始终具有与 JPEG、GIF 或 BMP 压缩格式相同的权重。它称为位图。因此,如果要计算内存中图像的大小,请获取宽度、高度和位大小以获得位权重,然后除以 8,即可得到字节位点。
请注意,可以从图像的某些部分(例如调色板图像)获得更多权重,它必须将图像调色板存储在内存中,但大多数时候这应该比位图权重小。
An image in memory always weights the same weight compressed in JPEG or GIF or BMP. It's called a BIT-MAP. So, if you want to calculate the size of an image in memory, take the width, the height and the bit size to get the bit weight, divide by 8 and you get the bytesite.
Note that it is possible to get more weight from an image in some parts such as a paletized image, it will have to store the image color palette in memory but most of the time this should weight less than a bitmap.
一个乘法。
只需将高度乘以宽度再乘以 3 即可。
并投入一些备用内存来处理所有这些字节。比如说图像大小的两倍。
但它与文件大小无关。
A multiplication.
Just multiply height by width by 3.
and throw in some spare memory to process all these bytes. say, twice the image size.
It has nothing to do with file size though.
在上传到服务器之前,你不能使用 php,
但你可以使用javascript!
https://developer.mozilla.org/en/DOM/File
将协同工作并将立即提供文件大小,请
阅读以下内容:
https://developer.mozilla.org/en/Using_files_from_web_applications
http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/
希望这有帮助!
before the upload on the server you can't with php,
but you can use javascript!
https://developer.mozilla.org/en/DOM/File
will work in conjunction of and will instantly provide the filesize using
read these:
https://developer.mozilla.org/en/Using_files_from_web_applications
http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/
hope this helps!
大小始终取决于格式。压缩图像时,图像本身的大小不会对文件大小产生任何影响。获得良好的文件大小读数的唯一方法是使用位图(或类似)格式。
例如,JPEG 的大小可能为 10MB,但由于压缩的工作原理,旋转时大小仅为 1MB。
The size will always depend on the format. When compressing images, the size of the image itself won't have any effect on the filesize. The only way that you can get a good readout of the filesize is if you use a Bitmap (or simmilar) format.
Example, A JPEG could be 10MB in size, and then when rotated only 1 MB in size because of how the compression works.