file_get_contents 从 URL 返回损坏的图像 [英] file_get_contents returning corrupted image from URL

查看:20
本文介绍了file_get_contents 从 URL 返回损坏的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码从 Mapquest Traffic API 下载图像文件.

I'm using this code to download image files from the Mapquest Traffic API.

<?php
$files = glob("/root/TrafficHistory/*.gif");
$lastFileName = $files[count($files)-1];

$newFile = time() . ".gif";

$imgData = file_get_contents("http://www.mapquestapi.com/traffic/v2/flow?key=**REMOVED**&mapLat=34.05396382838673&mapLng=-118.24529104634557&mapHeight=1000&mapWidth=1000&mapScale=433342");
file_put_contents("/root/TrafficHistory/" . $newFile, $imgData);

$md5Old = md5_file($lastFileName);

if ($md5Old == md5_file("./" . $newFile)) {
    unlink("./" . $newFile);
    echo "\033[31mTraffic data same at " . time() . " aborting.\033[0m\n";
} else {
    echo "\033[32mNew traffic data downloaded to /root/TrafficHistory/" . $newFile  . " at " . time() . "\033[0m\n";
}
?>

尽管我在保存图像时出现错误,但代码运行良好.如此处所示,您可以看到通过file_get_contents下载的流量叠加似乎没有完全下载 GIF 图像.这是一个 指向 GIF 的链接(它在浏览器中正确显示,但会未在 PhotoShop 中打开,说明文件意外结束).请注意,所有通过 file_get_contents 下载的图像都以这种方式显示.虽然当直接通过 API 链接查看时,它们显示完美.如果需要,这里还有一个指向 Mapquest Traffic API 的链接.感谢您的帮助.

The code works flawlessly though my error occurs while saving the image. As seen here you can see that the traffic overlay downloaded through file_get_contents does not seem to fully download the GIF image. Here's a link to the GIF in question (it display's correctly in the browser though will not open in PhotoShop stating unexpected end of file). Note that all of the images downloaded through file_get_contents display this way. Though when viewed directly through the API link they display perfectly. Here's a link to the Mapquest Traffic API as well, if it's needed. Thanks for your help.

推荐答案

您提供的文件肯定被截断了.它至少短 8 个字节.末尾有一个 GIF 图像数据块,声明长度为 168 字节,但此时文件中只剩下 162 字节.即使在那之后,至少应该有一个 0 终止符,然后是一个 GIF拖车"块(字节 0x3B).

The file you provided is definitely truncated. It's at least 8 bytes short. There's a GIF image data block at the end with a declared length of 168 bytes, but only 162 bytes remain in the file at that point. Even after that there should be, at a minimum, a 0 terminator and then a GIF "trailer" block (the byte 0x3B).

浏览器旨在呈现部分下载的图像,因此他们不会抱怨这一点,尽管您可以看到损坏:在触及图像最底部的道路上,有一条垂直线图案(Firefox)或不应该出现的水平线 (Chrome).

Browsers are designed to render partially downloaded images, so they don't complain about this, although you can see the corruption: on the road that touches the very bottom of the image, there is a pattern of vertical lines (Firefox) or horizontal lines (Chrome) that shouldn't be there.

如果您执行以下代码将 8 个字节附加到文件,它会清除损坏的位置,并且还可以在其他程序中打开文件:

If you execute the following code to affix eight bytes to the file, it clears up that spot of corruption and also becomes possible to open the file in other programs:

$data = file_get_contents('map.gif');
$data .= "\x00\x00\x00\x00\x00\x00" . "\x00" . "\x3B";
file_put_contents('map-repaired.gif', $data);

这是完成块的六个(空)字节,终止数据块的长度为 0 字节,然后是尾字节.但是,这种粗略的修复并不能作为所有其他图像的通用解决方案.

That's six (null) bytes to finish the block, the terminating data block's length 0 byte, then the trailer byte. Such a crude fix won't work as a general solution for all the other images though.

您提供的代码没有任何问题.我没有 API 的密钥,所以我无法实际测试它,但我只能想象 MapQuest 的末尾一定有一个错误.也许它的 GIF 压缩器坏了,或者服务器发送了一个不正确的 Content-Length HTTP 标头?我真的不知道.

There's nothing wrong with the code you've provided. I don't have a key for the API so I can't actually test it, but I can only imagine there must be a bug at MapQuest's end. Perhaps its GIF compressor is broken, or perhaps the server is sending an incorrect Content-Length HTTP header? I don't really know.

因为 MapQuest 可以返回 PNG 格式的图像,而且显然可以,所以就去做吧.PNG 压缩总是优于 GIF 压缩(除非图像非常小——大约 100 字节;然后更复杂的 PNG 格式的标头使其处于劣势)所以没有理由在这方面更喜欢 GIF案例.

Since MapQuest can return the images in PNG format, and since that apparently works, go for it. PNG compression is always superior to GIF compression (except in the case when the images are extremely tiny -- around 100 bytes; then the headers of the more complex PNG format put it at a disadvantage) so there is no reason to prefer GIF in this case.

这篇关于file_get_contents 从 URL 返回损坏的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆