简体   繁体   English

用PHP缓存生成的图像

[英]Image generated with PHP caching

I have a script that is generating an image with PHP on a server, in the form of image.png. 我有一个脚本,该脚本在服务器上使用PHP生成image.png形式的图像。 I'm then using this image in different places using <img src="http://domain.com/images/image.png" /> . 然后,我在<img src="http://domain.com/images/image.png" />不同位置使用此图像。

The problem I'm running into is that even though the image is being regenerated every 30 minutes, it seems to be cached and won't show the new values until I go to http://domain.com/images/image.png and then ctrl+shift+refresh . 我遇到的问题是,即使每30分钟重新生成一次图像,它也似乎已被缓存并且在我访问http://domain.com/images/image.png之前不会显示新值。然后ctrl+shift+refresh

Is there any way I can keep the image name the same, but make it always show the newest version of the image? 有什么办法可以使图像名称保持不变,但始终显示图像的最新版本?

This is happening as its cached by browser for more than 30 mins . 这是由于浏览器其缓存了30分钟以上而发生的。 As your image is generated every 30 mins, you should set the Expires and Cache-control header accordingly . 由于映像每30分钟生成一次,因此您应该相应地设置ExpiresCache-control标头

See these headers. 请参阅这些标题。

Expires: Mon, 10 Dec 2012 16:25:18 GMT
Cache-Control: max-age=1800

Here Expries is set to time 30 mins from now ( Date: Mon, 10 Dec 2012 15:55:18 GMT ). 这里的Expries设置为从现在开始30分钟的时间( Date: Mon, 10 Dec 2012 15:55:18 GMT )。 And Cache-Control also needs to be set. 并且还需要设置Cache-Control The unit is second here. 这里的单位是第二名。

I use these header for an image generation site where cache duration is 60 mins. 我将这些标头用于缓存持续时间为60分钟的图像生成站点。 These are the rules I follow to cache it. 这些是我遵循的缓存规则。

  1. Check if the image file exists 检查图像文件是否存在
    • If its older than my cache duration delete it and then generate new image. 如果它早于我的缓存持续时间,请删除它,然后生成新图像。
  2. If image file does not exists 如果图像文件不存在
    • generate the image and save it 生成图像并保存
  3. Now we have a valid image. 现在我们有了一个有效的图像。
  4. Calculate the file modification date of the image and add cache duration with it. 计算图像的文件修改日期,并为其添加缓存持续时间。
  5. Serve it with proper header where expires date will be the value we calculated on step 4. 将其与适当的标题一起使用,其中到期日期将是我们在第4步中计算出的值。

There are a few options, depending on your situation. 有几种选择,具体取决于您的情况。 If "images/image.png" is an actual file on the server and you are accessing it directly, then you have to change the cache settings on the folder or use .htaccess to inform a browser to resend it. 如果“ images / image.png”是服务器上的实际文件,并且您要直接访问它,则必须更改文件夹上的缓存设置,或使用.htaccess通知浏览器重新发送它。

<FilesMatch "\.(ico¦pdf¦flv¦jpg¦jpeg¦png¦gif¦js¦css¦swf)$">
ExpiresDefault A604800
Header set cache-control: "no-cache, public, must-revalidate"
</FilesMatch> 

If you are using PHP to find the image and returning it, you can use PHP to send headers. 如果使用PHP查找图像并返回图像,则可以使用PHP发送标头。

header("Expires: ".gmdate("D, d M Y H:i:s", time()+1800)." GMT");
header("Cache-Control: max-age=1800");

To do it perfectly with PHP, you can check if its actually modified 为了完美地使用PHP,您可以检查其是否真正被修改

$last_modified_time = @filemtime($file);
header("Expires: ".gmdate("D, d M Y H:i:s", $last_modified_time+1800)." GMT"); 
//change with $last_modified_time instead of time().
//Else if you request it 29mins after it was first created, you still have to wait 30mins
//but the image is recreated after 1 min.

header("Cache-Control: max-age=1800");
header("Vary: Accept-Encoding");

// exit if not modified
if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
    if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time) { 
        header("HTTP/1.1 304 Not Modified"); 
        return;
    }
}

You could try to load the image using PHP: 您可以尝试使用PHP加载图像:

<?php
//generateImage.php

$path = "xxxx/xxx.jpg";
$img =imagecreatefromjpeg($path);

header("Content-Type: image/jpeg");
imagejpeg($img);
imagedestroy($img);

?>

And then call the image like this: 然后像这样调用图像:

<img src="http://domain.com/generateImage.php" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM