开发者

Image cache won't display new edited image

开发者 https://www.devze.com 2023-03-29 06:26 出处:网络
I am build开发者_运维技巧ing a php script to write texts over an background image. I used GD functions like imagecopy(), imagejpeg(), imagedestroy() to merge save text image and the background image.

I am build开发者_运维技巧ing a php script to write texts over an background image. I used GD functions like imagecopy(), imagejpeg(), imagedestroy() to merge save text image and the background image. Everything is working perfectly. Upon form submit, the new image will be saved in the same file name of the background image and so on page reload, the edited image is not showing on the browser. It needs me to refresh the page using ctrl + F5(on windows) to load the edited image. Can anyone help me how to clear that cache?


just add ?v=something to background path everytime you edit your background image, it will force refresh


To handle image caching properly, you can write a rules either in your apache configuration or htaccess... or you can create simple "image provider", something like...

public function img($imgfile = '')
{
    $imgfile = $_GET['q'];
    $age = 60*60*24*31;
    $file = $_SERVER['DOCUMENT_ROOT'].'/'.$imgfile;

    if ( ! file_exists($file))
    {
       header('HTTP/1.0 404 Not Found');
    }
    else
    {
       $last_modified = filemtime($file);

        // Check for cached version
        if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) OR isset($_SERVER['HTTP_IF_NONE_MATCH'])) 
        {
           if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == gmdate('D, d M Y H:i:s \G\M\T', $last_modified)) 
           {
               header('HTTP/1.0 304 Not Modified');
               exit;
           }
        }   
        if(strpos($imgfile,'.png') !== FALSE)
        {
            Header('Content-Type: image/png');
        }
        elseif(strpos($imgfile,'.jpg') !== FALSE || strpos($img_file,'.jpeg') !== FALSE)
        {
            Header('Content-Type: image/jpg');
        }
        elseif(strpos($img_file,'.gif') !== FALSE)
        {
            Header('Content-Type: image/gif')
        }

        Header('Last-Modified : '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified));
        Header('Cache-Control : max-age='.$age.', must-revalidate');
        Header('Expires : '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified + $age));
        echo file_get_contents($file);
}

Then you can use that in your image tag, like <img src="provider.php?q=foo.jpg" alt="Foo" />

0

精彩评论

暂无评论...
验证码 换一张
取 消