Effective image caching?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Effective image caching?

Post by ben.artiss »

Hey everyone,

I've started caching CSS and JavaScript using .htaccess and a auto_prepend_file, and was wondering why the same technique isn't working for images? For example:

/css/.htaccess

Code: Select all

AddHandler application/x-httpd-php .css
php_value auto_prepend_file gzip-css.php
/css/gzip-css.php

Code: Select all

<?php 
ob_start('ob_gzhandler');
header('Content-type: text/css; charset: UTF-8');
header('Cache-Control: must-revalidate');
header('Expires: '.gmdate('D, d M Y H:i:s', time()+(60*60*24)).' GMT');
header('Last-Modified: '.gmdate("D, d M Y H:i:s", time()-(60*60*24)).' GMT'); // not too sure if this is naughty
?>
The code above works fine, however I'm trying the same approach for /img and I've hit a bit of a roadblock. Here's a sample for PNG:

/img/.htaccess

Code: Select all

AddHandler application/x-httpd-php .png
php_value auto_prepend_file gzip-png.php
/img/gzip-png.php

Code: Select all

<?php 
ob_start('ob_gzhandler');
header('Content-type: image/png');
header('Cache-Control: must-revalidate');
header('Expires: '.gmdate('D, d M Y H:i:s', time()+(60*60*24)).' GMT');
header('Last-Modified: '.gmdate("D, d M Y H:i:s", time()-(60*60*24)).' GMT');
?>
But that doesn't work, the images disappear from the page. Can anyone see what I'm doing wrong? Incidentally /img contains only directories - could this be the problem?

Thanks and regards, Ben
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Effective image caching?

Post by omniuni »

Try accessing an image directly when it should show up, and try to save it to your computer and view it with a hex editor. See if the data is actually getting delivered and has the wrong header, or if you are simply failing to send the image data somehow.
Post Reply