如何缓存php生成的图像

Jac*_*ani 6 php caching image

我制作了一个文件用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);
}
Run Code Online (Sandbox Code Playgroud)

但我的问题是一些图片缓存好,但其他人没有,有时候相册内的图片不会出现,直到我禁用缓存头

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

Jor*_*sen 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
Run Code Online (Sandbox Code Playgroud)

阅读本文档

示例#2:

ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
Run Code Online (Sandbox Code Playgroud)