#!/bin/bash ################################################################################ ## ## Generate Thumbnails and an index.html in a gallery-esque fashion ## Nicholas DeClario ## February 2011 ## $Id: tn,v 1.5 2011-02-19 13:53:48 nick Exp $ ## ## Please use the '--man' option for details on how this works. Running ## with no options will fall back to defaults and should work fine for ## most cases. The defaults are listed below; feel free to change them. ## ################################################################################ ## ## These are default options, feel free to manipulate these. ## HTML="index.html" TN_SIZE=64 MAX_COL=6 FNAMES=0 DIRECTORIES=0 TN_DIR=thumbs TITLE="`hostname`:`pwd`" FILE_TYPES='jpg png jpeg gif' VERSION="$Id: tn,v 1.5 2011-02-19 13:53:48 nick Exp $" ############################################################################### ## ## help( ) ## ## Display the basic help text ## ############################################################################### help( ) { cat <" When generating the HTML output the script will split the thumbnails up in to rows and columns. The number of rows varies as necessary but the number of columns or items per row can be specified with this option. The default is 6 items per row or 6 columns. .IP "--dir" Enabling this will allow the script to list the subdirectories that were found. It will place this list, with proper links in the HTML file. This is extremely handy when running this script recursivly. The default is off for this option. See below for running recursivly. .IP "--filenames" This will display the filename of the image below it's thumbnail in the HTML page. This is off by default. .IP "--help,?" Display the basic help menu .IP "--htmlfile filename.html" The output is all dumped to HTML and saved to a file, by default index.html. The default can be changed by using this option. .IP "--man,m" Display the detailed man page .IP "--size " This sets the default thumbnail size to pass to Imagemagick. The default is 64x64. .IP "--title " The page has a default title and header. This will statically set that header. .IP "--tndir <directory name>" The thumbnails can be saved in a seperate directory. This directory name may not exist in some areas making this a very handy option. "--version" Display the version .SH "SEE ALSO" man(1) convert(1) .SH "BUGS" .I Template.sh is a work in progress. Please report bugs. .SH "COPYRIGHT NOTICE" Copyright (c) 2006 Free Software Foundation, Inc. .br Copyright (c) 2011 Nicholas DeClario .PP Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. .ig Permission is granted to process this file through troff and print the results, provided the printed document carries copying permission notice identical to this one except for the removal of this paragraph (this paragraph not being relevant to the printed manual). .. .PP Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. .PP Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation. EOF exit 1; } ############################################################################### ## End of shell function definitions ############################################################################### ## ## We are going to read in our basic set of options and parse everything ## out. Setting NOARGS to 1 at the top of this script will tell getOpts ## to call 'help' if no arguments are supplied on the command line. ## ## ## Parse through our command line arguments here ## while [ $# -gt 0 ] do arg=$1 shift case $arg in --columns) MAX_COL=$1 shift ;; --dir) DIRECTORIES=1 ;; --filenames) FNAMES=1 ;; --htmlfile) HTML=$1 shift ;; --man) man break ;; --size) TN_SIZE=$1 shift ;; --title) TITLE=$1 shift ;; --tndir) TN_DIR=$1 shift ;; --version) echo "Version: $VERSION" break ;; *) help break ;; esac done ## ## Make sure imagemagick is installed ## CONVERT=/usr/bin/convert [ -x $CONVERT ] || \ { echo "ERROR: Imagemagick not found" exit 1 } [ -d $TN_DIR ] || mkdir $TN_DIR cat <<EOF>$HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>$TITLE</tERROR: itle> </head> <body bgcolor="#F2F2F2"> <h1>$TITLE</h1> <table align="center" border="0"> <tr> EOF TIFS=$IFS CNT=0 for i in $FILE_TYPES; do IFS=$(echo -en "\n\b") for n in `find -maxdepth 1 -type f -iname \*.$i`; do TN=`basename $n .$i`-tn.$i; $CONVERT -resize $TN_SIZE $n $TN_DIR/$TN [ $CNT -ge $MAX_COL ] && \ { echo -e "</tr>\n<tr>" >>$HTML CNT=0 } [ $FNAMES -eq 0 ] || \ IMAGE_NAME="<br/><font size=\"0\">`basename $n`</font>" cat <<EOF>>$HTML <td align="center" valign="middle"> <a href="$n"><img src="$TN_DIR/$TN" alt="$n" border="0" /></a>  $IMAGE_NAME </td> EOF CNT=$(( CNT + 1 )) done done ## ## Link directories if enabled ## IFS=$(echo -en "\n\b") [ $DIRECTORIES -eq 0 ] || \ { echo -e "</tr>\n<tr>\n<td colspan=\"$MAX_COL\">\n\t<ul>" for dir in `find . -maxdepth 1 -type d`; do dir=`basename $dir` [ "$dir" != "$TN_DIR" ] && [ "$dir" != "." ] && \ echo -e "\t\t<li><a href=\"$dir\">$dir</a></li>" done echo -e "\t</ul>\n</td>" } >> $HTML IFS=$TIFS cat <<EOF>>$HTML </tr> <tr> <td colspan="$MAX_COL"> <hr/> <center> <font size="0"> Generated on <b>`date`</b>.<br/> Generated with command <b>'$0 $*'</b> by <b>`whoami`</b>. </font> <p> <a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" border="0" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a> </p> </center> </td> </tr> </table> </body> </html> EOF