From 8e8cfb2fb27c2b217144e1efaa4137254d58ed3e Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 24 Aug 2009 13:17:40 +0000 Subject: Some gallery stuff. Change in default number of news to show, and a new Gallery module. --- utils/imagecache.php | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 utils/imagecache.php (limited to 'utils/imagecache.php') diff --git a/utils/imagecache.php b/utils/imagecache.php new file mode 100644 index 0000000..39c741e --- /dev/null +++ b/utils/imagecache.php @@ -0,0 +1,98 @@ + $maxwidth) $scale = $width / $maxwidth; + if($height / $scale > $maxheight) $scale = $height / $maxheight; + + $image_p = imagecreatetruecolor($width / $scale, $height / $scale); + imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width / $scale, $height / $scale, $width, $height); + + return $image_p; +} + +function errorImage($message) +{ + header("Content-type: image/png"); + $im = @imagecreate(8 + strlen($message) * 5, 20) + or die("Cannot Initialize new GD image stream"); + $background_color = imagecolorallocate($im, 0, 0, 0); + $text_color = imagecolorallocate($im, 233, 14, 91); + imagestring($im, 1, 5, 5, $message, $text_color); + imagepng($im); + imagedestroy($im); +} + +function getCachedImage($filename, $maxwidth, $maxheight) +{ + global $IMAGECACHE; + $fullfilename = $IMAGECACHE . "/" . $maxwidth . ":" . $maxheight . ":". urlencode($filename); + + // Test the storage dir + if(!file_exists($IMAGECACHE)) { + if(!mkdir($IMAGECACHE)) errorImage("Could not create directory: " . $IMAGECACHE); + } + if(!is_dir($IMAGECACHE)) errorImage($IMAGECACHE . " exists but is not a directory"); + if(!is_readable($IMAGECACHE) || !is_writeable($IMAGECACHE) || !is_executable($IMAGECACHE)) { + errorImage($IMAGECACHE . " exists but does not have the correct permissions. (r/w/x)"); + } + // end of dir test + + if(!file_exists($fullfilename)) { + + $url = parse_url($filename); + $filetype = strrchr($url["path"], '.'); + + if( true || ping($url["hostname"], 1000) != -1) { + + error_reporting(E_ERROR | E_PARSE); + + switch(strtolower($filetype)) { + case ".jpeg": + case ".jpg": + $image = imagecreatefromjpeg(urldecode($filename)); + if(!$image) errorImage("Could not read: ". $filename); + $image = rescale($image, $maxwidth, $maxheight); + imagejpeg($image, $fullfilename, 90); + break; + + case ".gif": + $image = imagecreatefromgif(urldecode($filename)); + if(!$image) errorImage("Could not read: ". $filename); + $image = rescale($image, $maxwidth, $maxheight); + imagegif($image, $fullfilename); + break; + + case ".png": + $image = imagecreatefrompng(urldecode($filename)); + if(!$image) errorImage("Could not read: ". $filename); + $image = rescale($image, $maxwidth, $maxheight); + imagepng($image, $fullfilename); + break; + + default: + if(!$image) errorImage("Unknown image type " . $filetype); + break; + } + + error_reporting(E_ALL ^ E_NOTICE); + + } + } + + header('Content-Description: File Transfer'); + header('Content-Type: image/jpeg'); + header('Content-Length: ' . filesize($fullfilename)); + header('Content-Disposition: inline; filename=' . basename($filename)); + readfile($fullfilename); +} + +?> \ No newline at end of file -- cgit v1.2.3