width = $w; $this->height = $h; $this->cut = $c; } }; function rescale_nocut($image, $maxwidth, $maxheight) { $width = imagesx($image); $height = imagesy($image); if($width <= $maxwidth && $height <= $maxheight) return $image; $scale = 1; if($width > $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 rescale($image, $width, $height, $cut) { if($cut == false) return rescale_nocut($image, $width, $height); $aspect = $width / $height; $w = imagesx($image); $h = imagesy($image); $image_p = imagecreatetruecolor($width, $height); $margin = 0; $scale = 1.0; if($w / $h > $aspect) { // Cut right and left sides $scale = $height / $h; $margin = ($w * $scale - $width) / 2; $margin /= $scale; imagecopyresampled($image_p, $image, 0, 0, 0 + $margin, 0, $width, $height, $w - $margin * 2, $h); } else { // Cut top and bottom $scale = $width / $w; $margin = ($h * $scale - $height) / 2; $margin /= $scale; imagecopyresampled($image_p, $image, 0, 0, 0, 0 + $margin / 2, $width, $height, $w, $h - $margin * 2); } 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, $mod, $cat) { $module = loadModule($mod); if(!$module) die(404); if(!method_exists($module, "getImageSize")) die(404); $size = $module->getImageSize($cat); $maxwidth = $size->width; $maxheight = $size->height; $cut = $size->cut; global $IMAGECACHE, $JPEG_CACHE_QUALITY; $cutstr = "uncut"; if($cut) $cutstr = "cut"; $fullfilename = $IMAGECACHE . "/" . $maxwidth . ":" . $maxheight . ":" . $cutstr . ":" . 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, $cut); imagejpeg($image, $fullfilename, $JPEG_CACHE_QUALITY); break; case ".gif": $image = imagecreatefromgif(urldecode($filename)); if(!$image) errorImage("Could not read: ". $filename); $image = rescale($image, $maxwidth, $maxheight, $cut); imagegif($image, $fullfilename); break; case ".png": $image = imagecreatefrompng(urldecode($filename)); if(!$image) errorImage("Could not read: ". $filename); $image = rescale($image, $maxwidth, $maxheight, $cut); 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)); header('Last-Modified: ' . date("r", filemtime($fullfilename))); readfile($fullfilename); } ?>