*/ /** * Directory thumnail maker * * Makes thumbnails of all images in directory. Saves thumbnails in * format _thumb_[size]_[bgcolor]_filename.gif * Thumbnails will be square. * * Usage: * mkthumbdir(string $dir, int $size, sting $color); * * Example: * mkthumbdir(); * Makes thumbnails of everything in the current directory. * A thumbnail of myself.jpg will be named _thumb_128_myself.gif * * Example: * mkthumbdir('/home/example/',32,'ffffff'); * Makes a 32x32 thumbnail with a white background of everything * in /home/example/ * A thumbnail of myself.jpg will be named _thumb_32_ffffff_myself.gif * * @param Directory (defaults to current directory) * @param Width and height (same number; defaults to 128 -> 128x128) * @param Color in hex form (defaults to '' -> transparent) * @return TRUE = success */ function mkthumbdir($dir='.',$size=128,$color='') { if ($color!=='' && (strlen($color)!=6 || !ctype_xdigit($color)) || intval($size)<=0) /* $size or $color is not valid */ return FALSE; $size=intval($size);$color=hexlower($color); /*sanitize $size and $color */ $files = @opendir($dir) or die('Cannot open specified directory.'); while (false !== ($file = @readdir($files))) { if (substr($file,0,1)!=='.') /* skip '.', '..', and any hidden file */ mkthumb($dir.'/'.$file,$size,$color); } @closedir($files); return TRUE; } /** * Thumnail maker * * Makes thumbnails of an image. Saves thumbnails in * format _thumb_[size]_[bgcolor]_filename.gif * Thumbnails will be square. * * Usage: * mkthumb(string $image, int $size, sting $color); * * Example: * mkthumb('myself.jpg'); * Creates a 128x128 thumbnail of myself.jpg named * _thumb_128_myself.gif * * Example: * mkthumb('dragon.png',64,'00ffff'); * Creates a 64x64 thumbnail with cyan background of dragon.png named * _thumb_64_00ffff_dragon.gif * * @param Directory (defaults to current directory) * @param Width and height (same number; defaults to 128 -> 128x128) * @param Color in hex form (defaults to '' -> transparent) * @return TRUE = success */ function mkthumb($file,$size=128,$color='') { /** sanity checks **/ if ($color!=='' && (strlen($color)!=6 || !ctype_xdigit($color)) || intval($size)<=0) /* $size or $color is not valid */ return FALSE; $size=intval($size); $color=hexlower($color); $ic=hexdec($color); if (is_dir($file) || preg_match('/^_thumb_[0-9]+(_[0-9a-f]{6})?_/', basename($file)) || (substr($file,-4)!='.gif' &&substr($file,-4)!='.png' && substr($file,-5)!='.jpeg' && substr($file,-4)!='.jpg')) return FALSE; /* skip non-images and thumbs */ $thumbn = dirname($file).'/_thumb_'.$size.($color?'_'.hexlower($color): '').'_'.substr(basename($file),0,strrpos(basename($file),'.')).'.gif'; if (file_exists($thumbn)) return FALSE; /** import to GD **/ if (substr($file,-4)=='.gif') { if (!$srci = @imagecreatefromgif($file)) return FALSE;} else if (substr($file,-4)=='.png') { if (!$srci = @imagecreatefrompng($file)) return FALSE;} else { if (!$srci = @imagecreatefromjpeg($file)) return FALSE;} /** make thumbnail **/ $srcw = imagesx($srci); $srch = imagesy($srci); /* get img dimensions */ $desti = imagecreatetruecolor($size,$size); if ($color) imagefilledrectangle($desti, 0,0, $size,$size, imagecolorallocate($desti, 0xFF&($ic>>0x10),0xFF&($ic>>0x8), 0xFF&$ic)); else /* transparent */ { imagefilledrectangle($desti, 0,0, $size,$size, imagecolorallocate($desti, 253,13,248)); imagecolortransparent($desti, imagecolorallocate($desti,253,13,248)); } if ($srcw >$srch) /* landscape */ imagecopyresampled($desti,$srci,0, round(($srcw-$srch)*$size/$srcw/2), 0,0,$size,round($srch*$size/$srcw), $srcw,$srch); else /* portrait */ imagecopyresampled($desti,$srci, round(($srch-$srcw)*$size/$srch/2),0, 0,0,round($srcw*$size/$srch),$size,$srcw,$srch); imagedestroy($srci); imagegif($desti,$thumbn); imagedestroy($desti); return TRUE; } /** * Turns hex lowercase * * Faster than strtolower */ function hexlower($str) { return strtr($str, 'ABCDEF', 'abcdef'); } /* * The following code makes a thumbnail of everything in the current * directory. Get rid of it if you don't want that to happen. */ mkthumbdir() or die('Error.'); echo 'Done.'; ?>