* Pass an array of images, with optional descriptions, to $input: * $a = array('IMG_2546.jpg', * 'IMG_2555.jpg', * 'IMG_2559.jpg' => 'Some Flowers' * ); * or a relative folder location; Thumbmaker caches the list of files so if you change the images, delete the cache file * include a function call where you want the images: * thumbmaker($a, array('title' => 'Day 2')); * The second parameter accepts an array with any of these elements: * 'show_filename' as bool: to show file names underneath each image (default false) * 'height' as int: force height (default 150) * 'columns' as int: number of table columns (default 3) * 'shrink_to_fit' as bool: scale width of image to that of height as well (e.g. 150px-high but 600px-wide images if false) (default true) * 'paging' as int: number of images to show per page (default no paging) * 'thumb_ext' as string: force the file extension for thumbnail (e.g. 'jpg') (default original file format) * 'cache_file_list' as false to prevent caching of the list of files (default true) * 'show_ident' as bool: to show ident/credit being displayed (default true) * @param array or string $input * @param array $params * */ function thumbmaker($input, $params = null) { // var_dump($input); $cache_path = 'thumbmaker/'; $folderlistcache_name = 'filelist'; // file of cache of folder contents //parse int vals ((int)$params['height'] > 0) ? $max_h = $params['height'] : $max_h = 150; ((int)$params['columns'] > 0) ? $cols = $params['columns'] : $cols = 3; //parse bool vals that are true by default ($params['cache_file_list'] == false) ? $params['cache_file_list'] = false : $params['cache_file_list'] = true; ($params['shrink_to_fit'] == false) ? $params['shrink_to_fit'] = false : $params['shrink_to_fit'] = true; ($params['show_ident'] == false) ? $params['show_ident'] = false : $params['show_ident'] = true; if (is_string($input)) { // var_dump($input); // $input is a folder, determine its file list // point cache_path to the file list $cache_path = $input . $cache_path; // var_dump($cache_path); } else if (!is_array($input)) { // $input not a folder, nor an array, bail echo 'Thumbmaker wasn\'t given an folder or image list!'; return; } // if thumb folder doesn't exist, make it if (!file_exists($cache_path)) { if (!mkdir($cache_path, 777)) { echo "Thumbmaker couldn't make the cache folder '$cache_path'. Your web host may not allow it."; } } if (is_string($input)) { if ($params['cache_file_list'] && file_exists($cache_path . $folderlistcache_name)) { // we want file list cached, and it was $filelist = file($cache_path . $folderlistcache_name); $filelist = unserialize($filelist[0]); } else { // file list wasn't cached, get it $handle = @opendir($input); //suppress error $filelist = array(); //being filelist if ($handle) { // keep going until all files in directory have been read while ($file = readdir($handle)) { // var_dump($file); // get extension and add in if it's an image $ext = substr($file, strrpos($file, '.')); // var_dump($ext); switch ($ext) { case '.gif': case '.jpg': case '.jpeg': case '.png': $filelist[] = $input . $file; } } // tidy up: close the handler closedir($handle); if ($params['cache_file_list']) { $handle = fopen($cache_path . $folderlistcache_name, 'w'); fwrite($handle, serialize($filelist)); fclose($handle); } } } } else { // filelist was given as input $filelist = $input; } if ($params['title'] > null) { echo "\n

{$params['title']}

\n"; } echo "\n"; if ($params['show_ident']){ echo "\n"; } $i = 0; if ((int)$params['paging'] > 0) { // echo 'Paging is not yet implemented. Sorry!'; } foreach ($filelist as $k => $v) { // get image data from array element if (is_string($k)) { // passed URL and a desc $url = $k; $desc = $v; } else { // passed just a url $url = $v; $desc = null; } if ($params['show_filename'] == true) { // show file name, but remove the path $name = substr($url, strrpos($url, '/') + 1); $desc = "($name)\n
\n$desc"; } if (!$desc) { $desc = ' '; } // get file name and type $ext = strrpos($url, '.'); $file = substr($url, 0, $ext); $ext = substr($url, $ext); if (!$ext) { // no ext found, bail break; } // start to make the row $i++; if ($i == 1) { // start of row echo "\n"; } elseif ($i > $cols) { // end of row, wrap round echo "\n\n"; $i = 1; } echo "\n"; flush(); //send output after each img is done } if ($i != $cols) { // pad out the table for($j = $i; $j < $cols;$j++) { echo "\n"; } //echo"\n"; } echo"\n"; echo "
powered by http://vodex.net/thumbmaker
\n"; // is it cached? // generate cache filename extension; force if in $params ($params['thumb_ext']) ? $cache_ext = $params['thumb_ext']:$cache_ext = $ext; // generate cache filename $cache_file = $cache_path . md5($file) . $cache_ext; if (!file_exists($cache_file)) { // file not found, make it switch ($ext) { case '.gif': $img = @imagecreatefromgif($url); break; case '.jpg': case '.jpeg': $img = @imagecreatefromjpeg($url); break; case '.png': $img = @imagecreatefrompng($url); break; default: echo "$ext found from $url but extension not recognised"; } if ($img) { // resize the img; get how much to scale it by $scale = imagesy($img) / $max_h; if ($params['shrink_to_fit'] == true) { // shrink to fit, if the scale factor for fitting by WIDTH to max_h is larger, use that instead $scale2 = imagesx($img) / $max_h; if ($scale2 > $scale) { $scale = $scale2; } } $scaled_img = imagecreatetruecolor(imagesx($img) / $scale, imagesy($img) / $scale); imagecopyresized($scaled_img, $img, 0, 0, 0, 0, imagesx($scaled_img), imagesy($scaled_img), imagesx($img), imagesy($img)); // save to file switch ($cache_ext) { case '.gif': if (function_exists("imagegif")) { $wrote = imagegif($scaled_img, $cache_file); break; } // GD version doesn't have GIF support, drop through to jpeg case '.jpg': case '.jpeg': $wrote = imagejpeg($scaled_img, $cache_file); break; case '.png': $wrote = imagepng($scaled_img, $cache_file); break; default: echo "$ext found from $url but extension not recognised"; } if (!$wrote) { echo "Could not write cache file $cache_file"; } } else { echo "$url not found."; // break; } } echo "\n\"\"/\n
\n$desc\n"; echo "
 
\n"; } ?>