enable/disable cache mode $cache_dir = "./cache"; // server directory -> must be writeable $cache_web_dir = "/fotos/cache"; // web directory $allow_remote = false; // allow remote located images; when setting true be sure to also set allow_url_fopen=on in your server php.ini $default_width = 160; // define default width $default_height = 120; // define default height // So if there's something you don't like, don't sit on the sidelines. Get involved and reform it. Evolution works better than revolution, after all! // imagecopyresampled can be used instead of imagecopyresized; its a bit slower, but ppl think there are better quality results - not my opinion ;-) // requiered argument $pic = $_REQUEST["pic"]; if (!$allow_remote) { if (preg_match('/^(http|ftp):/i', $pic)) { die("Remote images are not allowed."); } } // optional parameters $maxwidth = (isset($_REQUEST['width']) ? floor($_REQUEST['width']) : $default_width); $maxheight = (isset($_REQUEST['height']) ? floor($_REQUEST['height']) : $default_height); $getimg = @getimagesize($pic); // 0. width // 1. height // 2. typ: 1=GIF, 2=JPEG, 3=PNG // 3. html tag (e.g. 'width="111" height="24"') // preventing from accessing non gfx files. if ($getimg[2] < 1 || $getimg[2] > 3 || $getimg[2] == "") { die("No valid gfx-source."); } $oldwidth = $getimg[0]; $oldheight = $getimg[1]; // if original is smaller then or equal to new image if ($oldwidth <= $maxwidth && $oldheight <= $maxheight) { header("HTTP/1.1 302 Found"); header("Location: " . $pic); } else { // image should be resized $widthdif = $oldwidth / $maxwidth; $heightdif = $oldheight / $maxheight; $maxdif = max($widthdif, $heightdif); $newwidth = floor($oldwidth / $maxdif); $newheight = floor($oldheight / $maxdif); // caching enabled if ($cache) { $char_replace = array(".", "/"); $cache_dir = $cache_dir . "/" . str_replace($char_replace, "", dirname($pic)) . "w" . $newwidth . "h" . $newheight . basename($pic); $cache_web_dir = $cache_web_dir . "/" . str_replace($char_replace, "", dirname($pic)) . "w" . $newwidth . "h" . $newheight . basename($pic); if (file_exists($cache_dir)) { // yeah, cached content header("HTTP/1.1 302 Found"); header("Location: " . $cache_web_dir); die; } } $smallimg = imagecreatetruecolor($newwidth,$newheight); switch ($getimg[2]) { case 1: // gif image $bigimg = imagecreatefromgif($pic); imagecopyresized($smallimg, $bigimg, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight); if ($cache) { imagegif($smallimg, $cache_dir); header("HTTP/1.1 302 Found"); header("Location: " . $cache_web_dir); } else { header("Content-Type: image/gif"); imagegif($smallimg); } break; case 2: // jpg image $bigimg = imagecreatefromjpeg($pic); imagecopyresized($smallimg, $bigimg, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight); if ($cache) { imagejpeg($smallimg, $cache_dir); header("HTTP/1.1 302 Found"); header("Location: " . $cache_web_dir); } else { header("Content-Type: image/jpeg"); imagejpeg($smallimg); } break; case 3: // png image $bigimg = imagecreatefrompng($pic); imagecopyresized($smallimg, $bigimg, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight); if ($cache) { imagepng($smallimg, $cache_dir); header("HTTP/1.1 302 Found"); header("Location: " . $cache_web_dir); } else { header("Content-Type: image/png"); imagepng($smallimg); } break; } }