为imagejpeg / imagepng函数创建的图像指定缓存验证器 [英] Specify a cache validator for images created by imagejpeg/imagepng functions
问题描述
我们所知道的所有事情都可以通过向.htaccess文件中添加以下行来为图像指定缓存验证器:
All we know we can specify a cache validator for images by adding following lines to .htaccess file:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 year"
</IfModule>
.. and ..
.. and ..
<IfModule mod_headers.c>
<FilesMatch "\.(bmp|css|flv|gif|ico|jpg|jpeg|js|pdf|png|svg|swf|tif|tiff)$">
Header set Last-Modified "Mon, 31 Aug 2009 00:00:00 GMT"
</FilesMatch>
</IfModule>
但是,它将对真实的JPG或PNG文件有效。但是,问题是,如何为使用PHP代码和imagejpeg / imagepng函数实时生成的图像指定缓存验证器? (以上代码对其无效)
But, it will be effective for real JPG or PNG files. However, the question is, how to specify cache validator for images that are built with PHP codes and imagejpeg/imagepng functions on fly? (above codes are not effective for them)
PS:我尝试使用.htaccess文件模拟由PHP创建的图像的URL,就像真实图像一样(例如: http://example.com/1.jpg
,它是由PHP文件生成的,不是真正的.jpg图像),但仍收到缓存验证程序警告。
P.S: I have tried to simulate the URL of image created by PHP like a real image using .htaccess file (e.g: http://example.com/1.jpg
, which is generated by PHP file and is not a real .jpg image), but still receiving cache validator warning.
推荐答案
您可以在imagejpeg / imagepng函数之前添加PHP代码:
You can add the PHP code before imagejpeg/imagepng functions:
function TestModifiedSince($PageTimeStamp, $TextDatePage) {
if (isset($_SERVER["HTTP_CACHE_CONTROL"])) {return;}
if (!isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {return;}
$TestTime=strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]);
if (($TestTime - $PageTimeStamp) >= 0) {
header('Last-Modified: '. $TextDatePage, true, 304);
exit();
}
}
# hh mm ss MM DD YYYY
$DateUpdateBaseDef = mktime(00, 00, 00, 08, 31, 2009);
$TimeHeadUpdate = gmdate('D, d M Y H:i:s', $DateUpdateBaseDef).' GMT';
TestModifiedSince($DateUpdateBaseDef, $TimeHeadUpdate);
这篇关于为imagejpeg / imagepng函数创建的图像指定缓存验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!