如何在不下载图像(完整)的情况下确定图像的大小?
我正在寻找远程托管 JPG 的尺寸、宽度和高度。我已经看到如何通过下载完整图像来做到这一点。
但是,如果我可以通过仅下载足以获取此信息的内容来做到这一点,那就太理想了。
典型的图像大小为 200K,对于 JPG 来说,开始时只需读取几 K 就足够了:
curl_setopt($ch, CURLOPT_RANGE, "0-4096");
作为参考,我遵循了这些答案/评论,但无济于事(尽管它们可能在正确的轨道上):
有没有人能够将各个部分组合在一起(有或没有 ImageMagick)?
I'm looking to get the dimensions, width and height, of a remotely hosted JPG. I have seen how it is possible to do this by downloading the full image.
However, it would be ideal if I could do this by downloading only enough to just get this information.
Typical images are 200K in size and reading in just a few K might at the beginning might be enough for JPGs:
curl_setopt($ch, CURLOPT_RANGE, "0-4096");
For reference, I've followed these answers/comments but to no avail (though they might be on the right track):
Has anyone been able to put the pieces together (withor without ImageMagick)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样使用 ImageMagick 的
ping
功能怎么样:How about using ImageMagick's
ping
functionality like this:根据 从二进制文件中获取 JPEG 的图像大小,图像的大小未存储在文件中的精确位置。根据扩展名,您可以在文件中包含实际大小之前的一大堆数据,例如缩略图。
以下 C++ 代码应该是您自己的 PHP 实现的良好开端: http://www.64lines .com/jpeg-width-height。您可以通过像数组一样访问包含数据的字符串来轻松调整此代码:
myString{$i}
而不是data[i]
您应该加载相对较大的块数据并解析它,然后在必要时加载更多数据。或者,您可以逐块加载文件,但是大量连接带来的开销违背了不加载整个文件的目的。
不管怎样,我不确定这些好处是否值得你花在这上面的时间。现在200k已经不算什么了。
According to Getting Image size of JPEG from its binary, the size of the image isn't stored in a precise location in the file. Depending on the extension, you can have a whole bunch of data in the file before the actual size, a thumbmail for example.
The following C++ code should be a good start for your own PHP implementation : http://www.64lines.com/jpeg-width-height. You can easily adapt this code by accessing the string containing your data like an array :
myString{$i}
instead ofdata[i]
You should load a relatively large chunk of data and parse it, then load more if necessary. Or you can load the file block by block, but the overhead caused by the numerous connections defeat the purpose of not loading the entire file.
Anyhow, I'm not sure the benefits will justify the time you spend on this. 200k is nothing nowadays.
上述多诺霍答案的改进版本。此方法检索 png 和 gif 文件的较小字节范围,如此处。
An improved version of donohoe's answer above. This one retrieves smaller byte ranges for png and gif files as described here.
我设法回答了我自己的问题,并且包含了 PHP 代码片段。
唯一的缺点(至少对我来说)是,这会在使用 getImageSize 读取维度之前将部分图像下载写入文件系统。
对我来说,10240 字节是检查大小为 200 到 400K 的 jpg 图像的安全限制。
I managed to answer my own question and I've included the PHP code snippet.
The only downside (for me at least) is that this writes the partial image download to the file-system prior to reading in the dimensions with
getImageSize
.For me 10240 bytes is the safe limit to check for jpg images that were 200 to 400K in size.