【发布时间】:2012-02-23 10:16:51
【问题描述】:
效果很好。
但是,问题是图像更改但名称相同时。我的缓存系统识别出图像已更改并删除旧的缓存文件并创建新文件,然后从我自己的缓存中提供新的调整大小的缓存文件。但是我的系统或网络浏览器缓存有问题。
当图像更改时,我的脚本会发送 200 OK 标头(用于更改的文件)...但图像预览不会更改并且响应为 200 OK,直到我在浏览器中手动按 F5。然后它再次发送 200 OK 并更改图像,从那时起它发送 304 Not changed(应该如此)。但问题是我必须按 F5 才能更改浏览器缓存的图像...我不知道为什么 200 OK 不会更改浏览器缓存的文件...
代码:
<?php
// Image manipulation script
include("../../../Init.php");
if(!isset($_GET["File"]))
return;
$sFullFile = urldecode($_GET["File"]);
$pData = pathinfo($sFullFile);
// Options
$sActions = isset($_GET["Actions"]) ? $_GET["Actions"] : "";
$iMaxWidth = isset($_GET["MaxWidth"]) ? (int)$_GET["MaxWidth"] : 0;
$iMaxHeight = isset($_GET["MaxHeight"]) ? (int)$_GET["MaxHeight"] : 0;
$bSaveAspect = isset($_GET["SaveAspect"]) & $_GET["SaveAspect"] == "0" ? false : true;
// Cache control
$sPathCacheString = md5($pData["dirname"]);
$sFileCacheString = md5($pData["basename"]);
$pLastModTime = filemtime($sFullFile);
$sOptCacheString = md5($sActions . $iMaxWidth . $iMaxHeight . $bSaveAspect);
$sCacheFile = ImageCache::GetImageManipulatorCacheFileName($sFullFile, $sActions, $iMaxWidth, $iMaxHeight, $bSaveAspect);
header("Cache-Control: private, max-age=2592000, pre-check=2592000, must-revalidate");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822, strtotime(" 30 day")));
$sLastModString = gmdate("D, d M Y H:i:s \G\M\T", $pLastModTime);
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) >= $pLastModTime)
{
header("HTTP/1.0 304 Not Modified");
return;
}
else
{
$pImage = new Image($sFullFile);
@unlink($sCacheFile);
if(GetFlag("ResizeMax", $sActions))
{
if($pImage->GetWidth() > $iMaxWidth || $pImage->GetHeight() > $iMaxHeight)
$pImage->ResizeImage($iMaxWidth, $iMaxHeight, $bSaveAspect);
}
Cache::WriteFile($sCacheFile, $pImage->GetOutput());
header("Last-Modified: $sLastModString");
header("Content-type: " . $pImage->GetMIMEType());
$pImage->Destroy();
Cache::StreamOut($sCacheFile);
}
/*
Copyright (C) 2010. Determinanta
http://www.determinanta.hr
Part of WebSuite aplication platform. All rights reserved.
*/
?>
编辑:已解决
我解决了...问题是检查 $_SERVER["HTTP_IF_MODIFIED_SINCE"]...如果没有更改,我现在只是提供缓存文件。我花了整个上午才解决这个问题...感谢您的所有投入。干杯。
<?php
// Image manipulation script
include("../../../Init.php");
if(!isset($_GET["File"]))
return;
$sFullFile = urldecode($_GET["File"]);
$pData = pathinfo($sFullFile);
// Options
$sActions = isset($_GET["Actions"]) ? $_GET["Actions"] : "";
$iMaxWidth = isset($_GET["MaxWidth"]) ? (int)$_GET["MaxWidth"] : 0;
$iMaxHeight = isset($_GET["MaxHeight"]) ? (int)$_GET["MaxHeight"] : 0;
$bSaveAspect = isset($_GET["SaveAspect"]) & $_GET["SaveAspect"] == "0" ? false : true;
// Cache control
$sPathCacheString = md5($pData["dirname"]);
$sFileCacheString = md5($pData["basename"]);
$pLastModTime = filemtime($sFullFile);
$sOptCacheString = md5($sActions . $iMaxWidth . $iMaxHeight . $bSaveAspect);
$sCacheFile = ImageCache::GetImageManipulatorCacheFileName($sFullFile, $sActions, $iMaxWidth, $iMaxHeight, $bSaveAspect);
$sFullCacheFile = CACHE_PATH . "/" . $sCacheFile;
$bCacheNotModified = true;
if(!is_file($sFullCacheFile))
$bCacheNotModified = false;
else
{
if(filemtime($sFullCacheFile) != $pLastModTime)
$bCacheNotModified = false;
}
$sLastModString = gmdate("D, d M Y H:i:s \G\M\T", $pLastModTime);
if($bCacheNotModified)
{
header("HTTP/1.0 304 Not Modified");
return;
}
else
{
$pImage = new Image($sFullFile);
@unlink($sCacheFile);
if(GetFlag("ResizeMax", $sActions))
{
if($pImage->GetWidth() > $iMaxWidth || $pImage->GetHeight() > $iMaxHeight)
$pImage->ResizeImage($iMaxWidth, $iMaxHeight, $bSaveAspect);
}
Cache::WriteFile($sCacheFile, $pImage->GetOutput());
touch($sFullCacheFile, $pLastModTime);
header("Cache-Control: private, max-age=2592000, pre-check=2592000, must-revalidate");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822, 0));
header("Last-Modified: $sLastModString");
header("ETag: \"" . $sImageMD5 . "\"");
header("Content-type: " . $pImage->GetMIMEType());
$pImage->Destroy();
Cache::StreamOut($sCacheFile);
}
/*
Copyright (C) 2010. Determinanta
http://www.determinanta.hr
Part of WebSuite aplication platform. All rights reserved.
*/
?>
【问题讨论】:
标签: php caching browser-cache