Version: 0.9.2
Type: Full Script
Category: File Management
License: Other
Description: This funky little script can simply be uploaded to a folder on your PHP-savvy server, and (once you’ve created a sub-directory to store stuff) will magically create&save thumbnail images, and make your images browsable and sortable.
Since the thumbnails are saved, there is minimal load on your server, and you can happily keep uploading more images and extending your archive.
You can see a working example at http://www.mediumart.co.uk/shakib/images
<? /***************************************** ** ** ** IMAGE BROWSER ** ** ** ** original code by shakib of ** ** ** ** http://www.mediumart.co.uk ** ** ** *****************************************/ /* NOTES this script is intended for use in a directory empty of everything other than image files (jpg and png, not gif due to licensing restrictions) this script will automatically generate and save thumbnails of images that you upload, and display these first off with links to the full images NOTE - image handling with php is not the most efficient in the world - do not try and parse more than 2 or 3 images at a time. NOTE - Updated to work with PHP5 and Apache 2 USAGE: 1. upload this script to your images directory 2. upload a couple of sample images 3. create a sub-directory for the thumbnails ("thumbs" is the default) 4. browse to this script over http! COPYRIGHT: this script isn't copyright at all. you can use it, modify it, sell it, claim it as your own, whatever. if you *want* to have a link back to us at http://www.mediumart.co.uk that would be great :) */ /* CONFIG */ // maximum width of images (height will be scaled accordingly) $maxw = 100; // directory to place thumbnail images $thumbdir = "thumbs/"; // number of columns in table $cols = 4; // number of rows in table $rows = 4; // default sorting method ["date","size","name"] $df_sortby = "name"; // default ordering (ascending or descending) ["ASC","DESC"] $df_ascdsc = "ASC"; /* INITIALISATION */ // useful shortcut $url = $_SERVER["PHP_SELF"]."?"; // start point and ordering $start = ( isset($_GET["start"]) ) ? $_GET["start"] : 0; $sortby = ( isset($_GET["sortby"]) ) ? $_GET["sortby"] : $df_sortby; $ascdsc = ( isset($_GET["ascdsc"]) ) ? $_GET["ascdsc"] : $df_ascdsc; // allowed image types $sfxs = array(".jpg",".jpeg",".png"); // get the current directory path: $curdir = dirname(stripslashes($_SERVER["SCRIPT_FILENAME"])); // get folder delimiter for this system: $slash = ( strstr($curdir,"") ) ? "" : "/"; $thumbdir = $thumbdir; /* READ DIRECTORY */ // create array to hold filenames and information // $images[n][0] = name // $images[n][1] = date // $images[n][2] = size $images = array(); @ $handle = opendir($curdir); if ( !$handle ) { error("could not open directory!"); exit; } while ( $file = readdir($handle) ) { if ( isValid($file,$sfxs) ) { $tmp = array(); $tmp[0] = $file; $tmp[1] = filemtime($file); $tmp[2] = filesize($file); array_push($images,$tmp); // do we need to create a thumbnail? if ( !file_exists($thumbdir.$slash.$file) ) { createThumb($file); } } } // now sort the dir: usort($images,"sortme"); ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Mary</title> <style> body, td, input, select { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 0.8em; } a { font-weight: bold; } .img { font-size: 0.7em; border: 1px solid #666666; text-align: center; vertical-align: bottom; padding: 5px 2px 5px 2px; } body { background-color: #FFFF99; } </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head> <body> <h3 align="center">Mary</h3> <? /* CREATE TABLE */ $k = $start; $n = count($images); $ord = "&sortby=".$sortby."&ascdsc=".$ascdsc; if ( $start > 0 ) { $pn = $start - $rows*$cols; $prev = '<a href="'.$url.$ord.'&start='.$pn.'">prev</a>'; } else { $prev = "prev"; } if ( $start + $rows*$cols < $n) { $nn = $start + $rows*$cols; $next = '<a href="'.$url.$ord.'&start='.$nn.'">next</a>'; } else { $next = "next"; } if ( $n-$k < $rows*$cols ) { $rows = ceil(($n-$k)/$cols); } ?> <table width="<?=$cols*$maxw*1.4?>" border="0" align="center" bgcolor="#FFFF63"> <tr> <td colspan="<?=$cols?>" align="center"> <form name="f" action="<?=$_SERVER["PHP_SELF"]?>" method="GET"> <input name="start" type="hidden" value="<?=$start?>"> sort by <select name="sortby"> <option value="-1">-- sort by --</option> <option value="date"<? if ($sortby=="date") echo " SELECTED"?>>date</option> <option value="size"<? if ($sortby=="size") echo " SELECTED"?>>size</option> <option value="name"<? if ($sortby=="name") echo " SELECTED"?>>name</option> </select> <input name="ascdsc" value="ASC" type="radio"<? if ($ascdsc=="ASC") echo " CHECKED"?>> ascending <input name="ascdsc" value="DESC" type="radio"<? if ($ascdsc=="DESC") echo " CHECKED"?>>descending <input type="submit" value="re-sort"> </form> </td> </tr> <tr> <td colspan="<?=$cols?>"><hr size="5"></td> </tr> <tr> <td colspan="<?=$cols?>"> <table width="100%" bgcolor="#FFFF63"> <tr> <td><?=$prev?></td> <td align="right"><?=$next?></td> </tr> </table> </td> </tr> <? for ( $i=0; $i<$rows; $i++ ) { ?> <tr> <? for ( $j=0; $j<$cols; $j++ ) { ?> <td bgcolor="#FFCC33" class="img"> <? if ( $k < $n ) { ?> <a href="<?=$images[$k][0]?>" target="blank"> <img src="<?=$thumbdir.$images[$k][0]?>" border="0"><br /> <?=$images[$k][0]?> </a><br /> <?=round($images[$k][2]/1024)?>Kb, <?=date("d-m-y",$images[$k][1])?> <? } else { ?> <? } ?> </td> <? $k++; } ?> </tr> <? } ?> <tr> <td colspan="<?=$cols?>"><hr size="5"></td> </tr> <tr align="left"> <td colspan="<?=$cols?>"> <table width="100%" border="1"> <tr> <td><?=$prev?></td> <td><div align="right"> <?=$next?> </div></td> </tr> </table> <div align="left"></div> <div align="left"></div></td></tr> </table> </body> </html> <? /* USEFUL FUNCTIONS */ function isValid($f,$a) { $t = getSuffix($f); return ( in_array($t,$a) ) ? true : false; } function getSuffix($f) { $n = strrpos($f,"."); return substr($f,$n,strlen($f)-$n); } function createThumb($f) { // use max width config value global $maxw, $curdir, $copyfile, $thumbdir, $slash; $type = getSuffix($f); // png or jpeg? // either way get image if ($type==".png") { $input = imagecreatefrompng($f); } else { $input = imagecreatefromjpeg($f); } if ( !$input ) { error("not a valid image file :("); return false; } // get size ( [0]=width, [1]=height ) $tmp = getimagesize($f); if ( !$tmp ) { error("Could not get input image size"); return false; } // get width of new thumbnail by taking // the smaller value from max and tmp width $w = ($tmp[0]>$maxw) ? $maxw : $tmp[0]; // scale height according to width $h = $tmp[1] * ($maxw/$tmp[0]); // create output image @ $output = imagecreatetruecolor($w,$h); if ( !$output ) { error("could not create output image"); return false; } // copy big image over to thumbnail, and resize down imagecopyresized( $output,$input, 0,0, 0,0, $w,$h, $tmp[0],$tmp[1] ); $newfile = $thumbdir.$slash.$f; // do the outputting! if ( $type == ".png" ) { imagepng($output,$newfile); } else { imagejpeg($output,$newfile); } } function sortme($a,$b) { // setup global $sortby, $ascdsc; if ( $sortby == "name" ) { $n = 0; } elseif ( $sortby == "date" ) { $n = 1; } elseif ( $sortby == "size" ) { $n = 2; } $m = ( $ascdsc == "ASC" ) ? 1 : -1; if ( $a[$n] == $b[$n] ) return 0; return ($a[$n] > $b[$n]) ? $m : -1*$m; } function error($str) { echo '<div style="background-color:#ffffff;color:#660000;font-weight:bold">'.$str.'</div>'; } ?>