[thelist] using gzip to deliver images

Bob Meetin bobm at dottedi.biz
Thu Jun 12 20:36:53 CDT 2008


With Ken's help I got it straight how to gzip stylesheets and javascript and improve page speed. The savings can be truly dramatic! I can't figure out how to make this work similarly with images. I have tried several different options with the header, but no dice yet. I changed the header to:

header('Content-Type: text/css; charset: UTF-8');

Also tried a few other ideas I googled including:

<?php

ob_start('ob_gzhandler');
$fn = 'spice.jpg';

// Getting headers sent by the client.
$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($fn))) {
	// Client's cache IS current, so we just respond '304 Not Modified'.
	header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($fn)).' GMT', true, 304);
} else {
	// Image not cached or cache outdated, we respond '200 OK' and output the image.
	header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($fn)).' GMT', true, 200);
	header('Content-Length: '.filesize($fn));
	header('Content-Type: image/jpeg');
	print file_get_contents($fn);
}
?>

See a sample page that is working for the stylesheets and javascript at:

www.dottedi.biz/codesamples/scripts/gzip/index.php

Rather than the good little kitty displaying all I am getting is the 'alt' text.  Suggestions?

-- 
Bob




More information about the thelist mailing list