PHP: simple chart caching

247 views
Skip to first unread message

Jan

unread,
Dec 11, 2007, 12:32:03 PM12/11/07
to Google Chart API
Save this file as cache.php, create a writeable folder 'cache' in the
same directory. Now just request 'http://example.org/cache.php?...'
instead of 'http://chart.apis.google.com/chart?...' and all your
charts will only be requested from Google once, then served from this
simple cache.

<?php

$url = $_SERVER['REQUEST_URI'];
$parts = parse_url($url);
$query = $parts['query'];

$md5 = md5($query);
$image = @file_get_contents('cache/'.$md5.'.png');

if(!$image) {
$image = @file_get_contents("http://chart.apis.google.com/chart?".
$query);
$handle = fopen ('cache/'.$md5.'.png', "w");
fwrite($handle, $image);
fclose($handle);
}

header("Content-Type: image/png");
echo $image;

?>

This is just a quick hack, but it works. Have fun :)

- Jan Piotrowski
Reply all
Reply to author
Forward
0 new messages