3 # bpgallery.sh - builds a simple 'gallery' from a collection of images
4 # Copyright (C) 2004 Brett Parker <iDunno@sommitrealweird.co.uk>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 function bpgallery_default_head() {
27 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
28 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
30 <title>${TITLE}</title>
31 <link rel="stylesheet" href="${BASEURL}style.css" type="text/css" />
37 declare -rf bpgallery_default_head
39 function bpgallery_default_description() {
46 declare -rf bpgallery_default_description
48 function bpgallery_default_thumbsline() {
49 if [[ -z $caption ]]; then
55 <div class="thumbnail"><a href="$link"><img src="${ICONSDIRECTORY}/$filename" alt="$caption_alt" /></a><div class="caption">$caption</div></div>
59 declare -rf bpgallery_default_thumbsline
61 function bpgallery_default_tail() {
68 declare -rf bpgallery_default_tail
70 function bpgallery_default_stylesheet() {
75 font-family: sans-serif;
84 height: ${MAXHEIGHT}px;
105 div.navigation ul li {
120 declare -rf bpgallery_default_stylesheet
122 function bpgallery_default_page() {
123 $BPGALLERY_HEAD_FUNCTION
128 if [[ ! -z $linklocation ]]; then
129 linkstart="<a href='$linklocation'>"
135 <div class="navigation">
137 <li><a href='${BASEURL}${INDEXDOCUMENT}'>Thumbnails</a></li>
139 if [[ ! -z $previouspage ]]; then
140 $ECHOCOMMAND "<li><a href='${previouspage}'>Previous</a></li>"
142 $ECHOCOMMAND "<li>Previous</li>"
145 if [[ ! -z $nextpage ]]; then
146 $ECHOCOMMAND "<li><a href='${nextpage}'>Next</a></li>"
148 $ECHOCOMMAND "<li>Next</li>"
153 <div class="mainimage">
154 ${linkstart}<img src="${filename}" alt="${caption}" />${linkend}
157 $BPGALLERY_TAIL_FUNCTION
160 declare -rf bpgallery_default_page
162 function bpgallery_escape_url() {
180 function bpgallery_cleanup_directory_name() {
198 declare -rf bpgallery_escape_url
200 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
201 declare -r BPGALLERY_THEME
204 if [[ ! -z ${TITLE} ]] ; then
208 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
209 . /usr/local/etc/bpgallery/config 2>/dev/null
212 if [[ -e /etc/bpgallery/config ]] ; then
213 . /etc/bpgallery/config 2>/dev/null
216 if [[ -e $HOME/.bpgallery.rc ]]; then
217 . $HOME/.bpgallery.rc 2>/dev/null
220 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
221 . $1/.bpgallery.rc 2>/dev/null
224 if [[ -z ${BPGALLERY_THEME} ]]; then
225 BPGALLERY_THEME=default
228 if [[ ! -z ${BPGALLERY_THEME_DIR} ]] && \
229 [[ -e ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} ]]; then
230 . ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} 2>/dev/null
231 elif [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
232 . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
233 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
234 . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
235 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
236 . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
239 if [[ -z $TITLE ]]; then
240 TITLE="Photo Gallery"
243 if [[ -z $CONVERTTOOL ]]; then
245 if [[ $? != 0 ]]; then
246 echo "Can't find convert, exiting"
249 CONVERTTOOL=$(hash -t convert)
251 if [[ ! -x $CONVERTTOOL ]]; then
252 echo "Can't find convert at location $CONVERTTOOL, exiting"
257 if [[ -z $FINDCOMMAND ]]; then
259 if [[ $? != 0 ]]; then
260 echo "Can't find find, exiting"
263 FINDCOMMAND=$(hash -t find)
265 if [[ ! -x $FINDCOMMAND ]]; then
266 echo "Can't find find at $FINDCOMMAND, exiting"
271 if [[ -z $XARGSCOMMAND ]]; then
273 if [[ $? != 0 ]]; then
274 echo "Can't find xargs, exiting"
277 XARGSCOMMAND=$(hash -t xargs)
279 if [[ ! -x $XARGSCOMMAND ]]; then
280 echo "Can't find xargs at $XARGSCOMMAND, exiting"
285 if [[ -z $ECHOCOMMAND ]]; then
288 if [[ ! -x $ECHOCOMMAND ]]; then
289 echo "Can't find echo at $ECHOCOMMAND, exiting"
294 if [[ -z $SORTCOMMAND ]]; then
296 if [[ $? != 0 ]]; then
297 echo "Can't find sort, exiting"
300 SORTCOMMAND=$(hash -t sort)
302 if [[ ! -x $SORTCOMMAND ]]; then
303 echo "Can't find sort, exiting"
308 if [[ -z $IDENTIFYCOMMAND ]]; then
310 if [[ $? != 0 ]]; then
311 echo "Can't find indentify, exiting"
314 IDENTIFYCOMMAND=$(hash -t identify)
316 if [[ ! -x $IDENTIFYCOMMAND ]]; then
317 echo "Can't find identify at $IDENTIFYCOMMAND, exiting"
322 if [[ -z $HEADCOMMAND ]]; then
324 if [[ $? != 0 ]]; then
325 echo "Can't find head, exiting"
328 HEADCOMMAND=$(hash -t head)
330 if [[ ! -x $HEADCOMMAND ]]; then
331 echo "Can't find head at $HEADCOMMAND, exiting"
336 if [[ -z $GREPCOMMAND ]]; then
338 if [[ $? != 0 ]]; then
339 echo "Can't find grep, exiting"
342 GREPCOMMAND=$(hash -t grep)
344 if [[ ! -x $GREPCOMMAND ]]; then
345 echo "Can't find grep at $GREPCOMMAND, exiting"
350 if [[ -z $SEDCOMMAND ]]; then
352 if [[ $? != 0 ]]; then
353 echo "Can't find sed, exiting"
356 SEDCOMMAND=$(hash -t sed)
358 if [[ ! -x $SEDCOMMAND ]]; then
359 echo "Can't find sed at $SEDCOMMAND, exiting"
364 if [[ -z $WCCOMMAND ]]; then
366 if [[ $? != 0 ]]; then
367 echo "Can't find wc, exiting"
370 WCCOMMAND=$(hash -t wc)
371 WCCOMMAND="$WCCOMMAND -l"
373 if [[ ! -x $WCCOMMAND ]]; then
374 echo "Can't find wc at $WCCOMMAND, exiting"
377 WCCOMMAND="$WCCOMMAND -l"
380 if [[ -z $WIDTH ]]; then
384 if [[ -z $IMAGEEXTENSIONS ]]; then
385 IMAGEEXTENSIONS="jpeg jpg gif png";
388 if [[ -z $CAPTIONHEIGHT ]]; then
392 if [[ -z $OUTPUTHTML ]]; then
396 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
397 GENERATEPAGESFORFULLSIZE=0
400 if [[ -z $GENERATEPAGESFORMEDIUMSIZE ]]; then
401 GENERATEPAGESFORMEDIUMSIZE=1
404 if [[ -z $PAGESDIRECTORY ]]; then
407 removetrailingslashes=${PAGESDIRECTORY%/}
408 temp=$removetrailingslashes
409 while [[ $removetrailingslashes != $temp ]]; do
410 temp=$removetrailingslashes
411 removetrailingslashes=${temp%/}
413 PAGESDIRECTORY=$(bpgallery_escape_url "${PAGESDIRECTORY}")
416 if [[ -z $MEDIUMWIDTH ]]; then
420 if [[ -z $INDEXDOCUMENT ]]; then
421 INDEXDOCUMENT=index.html
424 if [[ -z $MEDIUMDIRECTORY ]]; then
425 MEDIUMDIRECTORY=medium
427 # Strip out anything that might be broken
428 MEDIUMDIRECTORY=$(bpgallery_cleanup_directory_name "$MEDIUMDIRECTORY")
431 if [[ -z $ICONSDIRECTORY ]]; then
434 # Strip out anything that might be broken
435 ICONSDIRECTORY=$(bpgallery_cleanup_directory_name "$ICONSDIRECTORY")
438 if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
439 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
441 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
444 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
445 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
447 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
450 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
451 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
453 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
456 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
457 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
459 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
462 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
463 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
465 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
468 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
469 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
471 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
476 for imageext in $IMAGEEXTENSIONS; do
477 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
480 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
481 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
486 $0 [--help|--version|<path to images>]
489 displays this help screen
491 displays the version and exits
493 This can also takes some environment variables, these will use defaults if not
497 the title of the gallery
500 the width to make the icons
503 the height to make the icons (sets up a bounding box with WIDTH)
506 set the width of images in the medium size pages
509 the height to make images in the medium size pages (sets up a
510 bounding box with MEDIUMWIDTH)
513 set the theme to use (described below)
516 set an extra location to look for themes
519 sets the script output to be wrapped in a <pre> block
521 GENERATEPAGESFORMEDIUMSIZE
522 generate medium sized images and pages
524 GENERATEPAGESFORFULLSIZE
525 decide wether to generate pages for the full size images or not
528 name of the index page (e.g. index.html)
531 TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
541 function check_dimensions() {
546 iconwidth=$($IDENTIFYCOMMAND -format "%wx%h" "$dir/$filename")
547 iconheight=${iconwidth//*x}
548 iconwidth=${iconwidth//x*}
549 # are the dimensions correctish?
551 if [ ! -z $wantedheight ]; then
552 if [ $wantedwidth -ne $iconwidth ] && [ $wantedheight -ne $iconheight ]; then
555 # make sure that both of the dimensions are smaller than they
557 if [ $iconwidth -gt $wantedwidth ] || [ $iconheight -gt $wantedheight ]; then
562 if [ $wantedwidth -ne $iconwidth ]; then
569 function generate_resized_images() {
570 $ECHOCOMMAND "Generating $2"
573 if [ $width != $height ]; then
579 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
580 imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS |
581 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} |
582 while read filename; do
583 filename=${filename//\"/\\\"}
584 imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
585 if [ ! -r "$2/$filename" ] ||
586 [ "$filename" -nt "$2/$filename" ] ||
587 [ $imagesizeright -ne 1 ]; then
588 $ECHOCOMMAND $filename
593 if [ $totalimages -eq 0 ]; then
594 # might as well exit at this point - there are no pictures
595 $ECHOCOMMAND "No images for the gallery - exiting."
598 # check if the directory exists and create it other wise
604 $ECHOCOMMAND "Can't write to $2 directory, exiting"
609 if [ $totalimages -eq $imagestoupdate ]; then
610 $ECHOCOMMAND "Regenerating all $2"
611 elif [ $imagestoupdate -eq 0 ]; then
612 $ECHOCOMMAND "No Updated $2, not regenerating"
615 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
618 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
619 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
620 while read filename; do
621 tempfilename=${filename//\"/\\\"/};
622 imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
623 if [ ! -r "$2/$tempfilename" ] \
624 || [ "$tempfilename" -nt "$2/$tempfilename" ] \
625 || [ $imagesizeright -ne 1 ]; then
626 $ECHOCOMMAND -n $filename
627 $ECHOCOMMAND -n -e "\000"
630 $XARGSCOMMAND -0 --verbose --max-procs=4 -I {} $CONVERTTOOL -resize $1 '{}' $2/'{}' 2>&1 | \
631 while read throwout; do
632 $ECHOCOMMAND done: $currentimage/$imagestoupdate images
633 currentimage=$((currentimage+1))
635 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
636 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
639 function generate_thumbs() {
640 if [ ! -z $HEIGHT ]; then
641 generate_resized_images "${WIDTH}x${HEIGHT}" $ICONSDIRECTORY
643 generate_resized_images $WIDTH $ICONSDIRECTORY
647 function generate_medium() {
648 if [ ! -z $MEDIUMHEIGHT ]; then
649 generate_resized_images "${MEDIUMWIDTH}x${MEDIUMHEIGHT}" $MEDIUMDIRECTORY
651 generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
655 function generate_pages() {
656 $ECHOCOMMAND "Generating Pages"
676 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
680 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
681 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
683 while read imagefilename; do
684 previousimage=$currentimage
685 currentimage=$nextimage
686 nextimage=${imagefilename#./}
689 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
690 if [[ $addlinks == 1 ]]; then
691 addlink=${currentimage}.html
693 addlink=${currentimage}${extra}.html
696 addlink=${currentimage}
699 previouspage=$currentpage
700 currentpage=$nextpage
701 if [[ $addlinks == 1 ]]; then
702 nextpage=${nextimage}${extra}.html
704 nextpage=${nextimage}.html
707 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
708 filename=../${extradir}${currentimage}
710 filename=${extradir}${currentimage}
712 if [ "x$currentpage" != "x" ]; then
713 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
715 $ECHOCOMMAND $nextimage
721 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
722 if [[ $addlinks == 1 ]]; then
723 addlink=${currentpage}.html
725 addlink=${currentpage}${extra}.html
728 addlink=${currentpage}
731 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
732 filename=../${extradir}${currentpage}
734 filename=${extradir}${currentpage}
736 if [[ $addlinks == 1 ]]; then
737 currentpage=${currentpage}${extra}.html
738 previouspage=${previouspage}${extra}.html
740 currentpage=${currentpage}.html
741 previouspage=${previouspage}.html
744 generate_general_page "$previouspage" "$currentpage" "" $addlink
748 function generate_medium_pages() {
749 generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
752 function generate_general_page() {
767 if [[ ! -z $4 ]]; then
771 if [ -r captions.txt ]; then
772 imagefilename=${filename##*/}
773 caption=$($GREPCOMMAND -E "^${imagefilename} " captions.txt); caption=${caption#* }
778 $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
781 if [[ $OUTPUTHTML != 0 ]]; then
786 $ECHOCOMMAND "No path given"
791 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
796 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
801 if [[ ! -d $1 ]]; then
802 $ECHOCOMMAND "$1 is not a directory"
809 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
810 $ECHOCOMMAND "$1 does not contain any images. Quitting."
815 $ECHOCOMMAND "Can't write to images directory, exiting"
819 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
820 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
824 if [ -e style.css ] && [ ! -w style.css ]; then
825 $ECHOCOMMAND "Can't write style.css, exiting"
831 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
832 if [ ! -d $PAGESDIRECTORY ]; then
833 mkdir -p $PAGESDIRECTORY
836 if [ ! -w $PAGESDIRECTORY ]; then
837 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
840 UNIXPAGESDIRECTORY=$PAGESDIRECTORY/
841 PAGESDIRECTORY=$(bpgallery_escape_url $PAGESDIRECTORY)/
846 UNIXPAGESDIRECTORY=""
849 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
851 generate_medium_pages
854 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
855 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
856 generate_pages __${MEDIUMDIRECTORY}
862 if [ -r description.txt ] ; then
863 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
870 $ECHOCOMMAND "Starting to generate page"
872 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
873 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
875 $ECHOCOMMAND "Adding Captions"
878 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
879 extra=__${MEDIUMDIRECTORY}
882 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $SORTCOMMAND -g | while read filename; do filename=${filename#./}; if [ -r captions.txt ]; then caption=$($GREPCOMMAND -E "^$filename " captions.txt); caption=${caption#* }; else caption=""; fi; if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then link=${PAGESDIRECTORY}$(bpgallery_escape_url "${filename}${extra}.html"); elif [ $GENERATEPAGESFORFULLSIZE != 0 ]; then link=${PAGESDIRECTORY}$(bpgallery_escape_url "${filename}.html"); else link=$(bpgallery_escape_url "$filename"); fi; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> ${INDEXDOCUMENT}
884 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
886 $ECHOCOMMAND "Finished generating the page"
887 $ECHOCOMMAND "Generating stylesheet"
890 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
891 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
895 # add a bit to the maxheight for the size of the caption
896 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
898 $BPGALLERY_STYLESHEET_FUNCTION > style.css
900 $ECHOCOMMAND "All done"
902 if [[ $OUTPUTHTML != 0 ]]; then
903 $ECHOCOMMAND "</pre>"