문제

I have a script that is generating an image with PHP on a server, in the form of image.png. I'm then using this image in different places using <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.

Is there any way I can keep the image name the same, but make it always show the newest version of the image?

도움이 되었습니까?

해결책 2

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.

<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.

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

$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;
    }
}

다른 팁

This is happening as its cached by browser for more than 30 mins. As your image is generated every 30 mins, you should set the Expires and Cache-control header accordingly.

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). And Cache-Control also needs to be set. The unit is second here.

I use these header for an image generation site where cache duration is 60 mins. 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.

You could try to load the image using 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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top