Mega Code Archive

 
Categories / Php / Graphics
 

Im cache

<?php function imcache($cachepath, $type, $function) { // Compose a uniqe'ish filename for cacheing. $filename = urlencode($_SERVER["REQUEST_URI"]) . "---" . urlencode($function); $filepath = realpath($cachepath)."/".$filename; if(file_exists($filepath) and filectime($_SERVER["PATH_TRANSLATED"]) < filectime($filepath)) { // if a file is there - stream response directly header("Content-type: ".image_type_to_mime_type(exif_imagetype($filepath))); @readfile($filepath); touch($filepath); } else { // if a file isn't there - call function that wil generate image // first make sure that $function "looks" like a function call if(ereg("^\s*[_a-z0-9-]+\s*\(.*\)\s*;?\s*$", $function)) { $im = FALSE; // adjust function exspression ... (it needs to start with "$im = " and end with ";") if(!(ereg(";\s*$", $function))) $function = $function.";"; $function = '$im = '.$function; // black magic ;-) eval($function); header ("Content-type: image/$type"); switch($type) { case "gif": imagegif($im); imagegif($im, $filepath); break; case "jpeg": imagejpeg($im); imagejpeg($im, $filepath); break; case "png": imagepng($im); imagepng($im, $filepath); break; } imagedestroy($im); } else { trigger_error('imcache: $function argument not valid, must be in the form "foo(arg, arg)" on line '. __LINE__ ." in ". __FILE__); } } } ?>