expires header on php generated image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • henryrhenryr
    New Member
    • Jun 2007
    • 103

    expires header on php generated image

    I am trying to make my php-generated images cache. Here is a summary of the script with the headers. It doesn't seem to cache though. Do I need to do something special to make sure it caches?

    [code=php]
    //summarised

    //resize...
    $file= $_GET['file'];
    $o= imagecreatefrom gif($file);
    $i= imagecreatetrue color($w,$h);
    imagecopyresamp led($i,$o,0,0,0 ,0,$w,$h,$wo,$h o);

    //output
    header('Content-type: image/gif');
    header('Expires : '.date(DATE_RFC 1123,time()+259 2000));
    imagegif($i);
    imagedestroy($i );

    [/code]

    Here are the response headers (using firebug). The server clearly gets the expires header. But I still have the overhead showing in firebug.

    Date Tue, 29 Jul 2008 13:06:36 GMT
    Server Apache/2.0.55 (Ubuntu) PHP/5.1.2
    X-Powered-By PHP/5.1.2
    Expires Thu, 28 Aug 2008 14:06:36 BST
    Content-Length 2182
    Keep-Alive timeout=15, max=95
    Connection Keep-Alive
    Content-Type image/gif
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Henry.

    I hate to just drop an article on you, but I have to run out the door in a sec. In the meantime, perhaps this article will have some information that you might find useful (http://www.mnot.net/cache_docs/).

    And I am now subscribed to this thread (:

    Comment

    • henryrhenryr
      New Member
      • Jun 2007
      • 103

      #3
      Originally posted by pbmods
      I hate to just drop an article on you,
      That's fine! I'd seen this one but it seems like it was one of those occasions where I just didn't type the right thing into google yesterday!

      I just found this:


      The solution is to use the last-modified header based on the filemtime() for the input file. The article has a solution if you have no input file too. The browser now caches the images. I hope it will refresh when they are updated!

      [code=php]
      //summarised
      //two additions - check last-modified header and exit if same as
      //filemtime(), also set last-modified header

      //resize...
      $file= $_GET['file'];

      if (isset($_SERVER['HTTP_IF_MODIFI ED_SINCE']) && strtotime($_SER VER['HTTP_IF_MODIFI ED_SINCE'])==filemtime($f ile)) {
      header('Last-modified: '.date(DATERFC_ 1123,filemtime( $file)),true,30 4);
      exit;
      }

      $o= imagecreatefrom gif($file);
      $i= imagecreatetrue color($w,$h);
      imagecopyresamp led($i,$o,0,0,0 ,0,$w,$h,$wo,$h o);

      //output
      header('Content-type: image/gif');
      header('Expires : '.date(DATE_RFC 1123,time()+259 2000));
      header('Last-modified: '.date(DATE_RFC 1123,filemtime( $file);
      imagegif($i);
      imagedestroy($i );

      [/code]

      Comment

      Working...