【发布时间】:2016-03-08 20:57:13
【问题描述】:
我有一个被 php 脚本分割的图像。我这样称呼它。
<img src="/index.php/image-name.jpg">
如果图像超过 5 分钟,我的脚本将从数据提供程序检索图像的新副本,然后显示新图像。
当提供图片的网站有负载并且此脚本去获取新副本时,它通常只会显示图片的顶部。 Firebug 会告诉我图像已损坏或被截断。如果我在新选项卡中打开图像,我的服务器就有完整副本。如果我在 5 分钟内第二次运行该脚本,它将完美运行。
看起来如果获取图像需要超过一定的时间,它就会失败并且只显示顶部。关于如何让它在放弃之前等待更长时间的任何想法?或者我可能完全走错了方向。
<?php
// get the image name from the uri
$path= $_SERVER['REQUEST_URI'];
$image = explode("/", $path);
$image=$image[3];//Get the file name
$image=str_replace('%20',' ', $image); //make it all spaces
$localimage='./road_images/'.$image; //where to find the image on the sever
// check if the image exists, this prevents some kinds of attacks
if (is_file($localimage)) {
$age = filemtime($localimage); // get the file age
if ($age < time() - (60*5)) { // 5 mins old
$simage='http://www.someplace/cams/'.$image;
$simage=str_replace(' ', '%20', $simage);//need to remove the spaces for URLs
copy($simage, $localimage);
}
// serve the image to the user.
$fp = fopen($localimage, 'r');
// send the right headers
header("Content-Type: image/jpg");
header("Content-Length: " . filesize($localimage));
// dump the picture and stop the script
fpassthru($fp);
exit();
}
else
{
echo("Error, no such file: '$image'");
}
?>
编辑:通过编辑发现了这一点
header("Content-Length: " . filesize($localimage));
它按预期工作。仍在试图找出原因。
【问题讨论】:
-
我正在镜像另一个不允许我盗链的网站。图像每 5 分钟更新一次。如果我的图像副本超过 5 分钟,我会去获取一个新副本并显示它。如果它不到 5 分钟,那么我使用我已经拥有的副本。
-
哦.. 你知道文件大小吗?可能文件对于您的 Web 服务器来说太大了。 (忽略我最后的评论)
-
图片很小。一个大的将是 328 × 314 像素和 30KB。晚上大部分图像是黑色或接近黑色,接近 10KB。