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

查看:84
本文介绍了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"trailer"块(字节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天全站免登陆