EZ Thumbnailer
From WorkCDN
Here is a nice and easy image thumbnailer and simple gallery script for apache webservers written in PHP. Licensed BSD style. Feel free to use it.
File: thumb.php
<?php /* * Copyright (c) 2009, Sam Washburn * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the * following conditions are met: * * + Redistributions of source code must retain the above copyright notice, this list of conditions and the following * disclaimer. * + Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided with the distribution. * + Neither the name of the Sam Washburn nor the names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ /* * DOCUMENTATION * * + ABOUT THE SCRIPT * This script generates and optionally caches image thumbnails based upon files located in the same directory as the * script. This script is designed to only work with apache webservers that have the mod_rewrite module enabled. * * + INSTALLATION AND OPERATION * 1. Modify the Settings below to suit your needs. * 2. Create a directory on your webserver and copy the thumb.php file into that directory. * 3. Run the script once from your browser to set up the .htaccess and optional cache directory * NOTE: You may have problems if an .htaccess file already exists. The script will not delete modify or overwrite * an existing .htaccess file. * 4. Upload pictures to the directory * 5. View full picture like this: http://www.mydomain.com/path/to/thumbs/picture.jpg * 6. View a thumbnail like this: http://www.mydomain.com/path/to/thumbs/picture.jpg?xs * (where 'xs' is the chosen size from the $sizes array in the settings) * 7. Optionally you can view a numerically sized thumbnail (if enabled) like this: * http://www.mydomain.com/path/to/thumbs/picture.jpg?100 * (where '100' is the maximum width and height of the picture) * 8. Optionally you can view a simplistic gallery of all the pictures in the folder like this: * http://www.mydomain.com/path/to/thumbs/ * (if $allowIndexBrowsing is enabled) * * VERSION HISTORY * 0.1 Initial Release * */ /** * This array defines the allowed size tags for the thumbs and their respective max pixel width/height. * This will be the max width for landscape oriented pictures and the max height for portrait oriented pictures */ $sizes = array( 'xs' => 80, 's' => 160, 'm' => 320, 'l' => 640, 'xl' => 1024 ); /** * If index browsing is enabled you can view a simplistic gallery of all the pictures in the folder. */ $allowIndexBrowsing = true; /** * This is the size used to display the pictures in the index gallery */ $indexThumbSize = 'xs'; /** * This is the number of columns of pictures in the index gallery */ $indexThumbCols = 4; /** * If this option is enabled numeric sizing urls are allowed. See point 7. in the installation docs above. */ $allowNumericSizing = false; /** * If this options is enabled thumbnails will be cached to a folder called "cache" in the image directory */ $useCache = false; /** * If caching is enabled this is the time-to-live for a cached item. It is expressed in seconds. */ $cacheTTL = 86400; // 1 day (in seconds) /** * END OF SETTINGS * No need to modify below this point, unless you really want to monkey with things. */ $fileName = $_GET['fn']; $scriptName = basename(__FILE__); if (!function_exists('file_put_contents')) { function file_put_contents($filename, $data) { if (($h = fopen($filename, 'w')) === false) { return false; } if (($bytes = fwrite($h, $data)) === false) { return false; } fclose($h); return $bytes; } } if(!file_exists(dirname(__FILE__).'/.htaccess')) { file_put_contents('.htaccess', <<<EOF RewriteEngine on RewriteRule ^(.*)$ thumb.php?fn=$1 [QSA,L] EOF ); } if(empty($fileName)) { if($allowIndexBrowsing) { $dirh = opendir(dirname(__FILE__)); if ($dirh) { $c = 0; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="style.css" rel="stylesheet" type="text/css"> <title>Images</title> </head> <body> <h2>Image Listing</h2> <? while ($de = readdir($dirh)) { switch($de){ case '.': case '..': case '.htaccess': case 'cache': case $scriptName: // Do nothing break; default: ?> <a href="<?=$de?>"><img src="<?=$de?>?<?=$indexThumbSize?>" /></a> <? $c++; if(($c % $indexThumbCols)==0) echo "<br/>"; } } unset($de); closedir($dirh); ?> </body> </html> <? exit(); } else { die('Cannot access directory.'); } } else { die('Script denies permission.'); } } // find the size $size = null; foreach ($_GET as $param=>$val) { if($allowNumericSizing) if(is_numeric($param)) $size = $param; if(isset($sizes[$param])) $size = $sizes[$param]; } if(is_null($size)) { // no size found.. output original image if(strstr($fileName,'.')!==false){ $ext = strtolower(array_pop(explode('.',$fileName))); switch($ext){ case 'jpg': case 'jpeg': $type = 'image/jpeg'; break; case 'gif': $type = 'image/gif'; break; case 'png': $type = 'image/png'; break; default: die('MIME type not supported for this file.'); } header("Content-type: {$type}"); } readfile(dirname(__FILE__).'/'.$fileName); exit(); } // Build a thumb header("Content-type: image/jpeg"); $rebuildCache = false; if($useCache) { if(!file_exists('cache')) mkdir(dirname(__FILE__).'/cache'); $md5 = md5($size.'-'.$fileName); $cache = "cache/{$md5}.jpg"; $rebuildCache = true; if(is_file($cache)===true){ $lastMod = filemtime($cache); if((time()-$lastMod) <= $cacheTTL) { readfile($cache); exit(); } } } $image = @file_get_contents(dirname(__FILE__).'/'.$fileName); if(strlen($image)==0) die ("Cannot read picture file."); $l = imagecreatefromstring($image); if(imagesx($l)>imagesy($l)) { $width = $size; $height = (imagesy($l) * $size) / imagesx($l); } else { $height = $size; $width = (imagesx($l) * $size) / imagesy($l); } $im = imagecreatetruecolor($width,$height); imagecopyresampled($im, $l, 0, 0, 0, 0, imagesx($im), imagesy($im), imagesx($l), imagesy($l)); if($rebuildCache){ imagejpeg($im,$cache,80); readfile($cache); } else { imagejpeg($im,null,80); } imagedestroy($im);
