如何缓存php生成的图像

时间:2011-12-29 17:36:06

标签: php image caching

我制作了一个文件用W打印出图像文件,我用get方法定义

但我的问题是缓存这些图片

我将此标题添加到文件

@header("Cache-Control: private, max-age=10800, pre-check=10800");
@header("Pragma: private");
@header("Expires: " . date(DATE_RFC822,filemtime($full_path)));

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
       &&
  (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($full_path))) {
  // send the last mod time of the file back
  header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($full_path)).' GMT',true, 304);
  exit;
}else
{
     @header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($full_path)) . ' GMT');
     @header('Content-Type: image/jpeg');
     @imagejpeg($image);
}

但我的问题是有些图片缓存正常,但其他图片没有,有时候相册中的图片不会出现,直到我禁用缓存标题

我的标题是否正确? ,关于缓存,我必须使用 - 或+来设置时间缓存如何工作?

1 个答案:

答案 0 :(得分:1)

因为他们将Htaccess与mod_expires一起使用?

示例#1:

# enable expirations
ExpiresActive On
# expire GIF images after a month in the client's cache
ExpiresByType image/gif A2592000
# HTML documents are good for a week from the
# time they were changed
ExpiresByType text/html M604800

Read This Documentation

示例#2:

ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000