FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » FUDforum » FUDforum Suggestions » Caching images (Would be nice if images are cached, so they do not need to reload all the time)
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Caching images [message #36381] Tue, 20 March 2007 15:52
magli is currently offline  magli   Italy
Messages: 15
Registered: February 2007
Location: Meran, Italy
Karma:
Junior Member
If images which are retrieved on the browser side get cached, it can often speed up the time it takes for a page to load. This helps a lot on my forum, where people post a lot of picture.

I changed the following at the end of src/getFile.php.t:

header('Content-Type: '.$r[0]);    
header('Content-Disposition:'.$append.'filename="'.$r[1].'"');
header('Content-Length: '.array_pop($r));
@readfile($r[2]);

to this:
if ($r[0]=="image/jpeg" || $r[0]=="image/png" || $r[0]=="image/tiff")
{
        $size = getimagesize($r[2]);
        if ($size[0]>800) system("mogrify -resize 800 ".$r[2]);
        $headers = apache_request_headers();

        // Checking if the client is validating his cache and if it is current.
        if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == filemtime($r[2]))) {
                // Client's cache IS current, so we just respond '304 Not Modified'.
                $expires = 60 * 60 * 24 * 30;
                $exp_gmt = gmdate("D, d M Y H:i:s", time() + $expires )." GMT";
                //header("Expires: {$exp_gmt}");
                header("Cache-Control: public, max-age={$expires}");
                header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($r[2])).' GMT', true, 304);
        } else {
    //Image not cached or cache outdated, we respond '200 OK' and output the image.
                $expires = 60 * 60 * 24 * 30;
                $exp_gmt = gmdate("D, d M Y H:i:s", time() + $expires )." GMT";
                header("Cache-Control: public, max-age={$expires}");
                header("Expires: {$exp_gmt}");
                header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($r[2])).' GMT', true, 200);
                header('Content-Length: '.filesize($r[2]));
                header('Content-Disposition: '.$append.'filename="'.$r[1].'"');
                header('Content-Type: '.$r[0]);
                print file_get_contents($r[2]);
        }
} else {
        header('Content-Type: '.$r[0]);
        header('Content-Disposition: '.$append.'filename="'.$r[1].'"');
        header('Content-Length: '.array_pop($r));
        @readfile($r[2]);
}

Now if I re-load a page containing an image which I have already downloaded, I do not have to re-load it.

Note that if your php is NOT running as an apache mod, you will not have the apache_request_headers() function - in which case, you also need to add this code:
if( !function_exists('apache_request_headers') ) {
///
function apache_request_headers() {
  $arh = array();
  $rx_http = '/\AHTTP_/';
  foreach($_SERVER as $key => $val) {
   if( preg_match($rx_http, $key) ) {
     $arh_key = preg_replace($rx_http, '', $key);
     $rx_matches = array();
     // do some nasty string manipulations to restore the original letter case
     // this should work in most cases
     $rx_matches = explode('_', $arh_key);
     if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
       foreach($rx_matches as $ak_key => $ak_val ) $rx_matches[$ak_key] = ucfirst( $ak_val);
       $arh_key = implode('-', $rx_matches);
     }
     $arh[$arh_key] = $val;
   }
  }
  return( $arh );
}

I think this sort of cache-control would be a good addition to FUDforum.

cheers.
[Message index]
 
Read Message
Previous Topic: Adding a forum link to the mail sent to mailing list
Next Topic: IP in action log
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 12:09:36 GMT 2024

Total time taken to generate the page: 0.04426 seconds