3 # bpgallery.sh - builds a simple 'gallery' from a collection of images
4 # Copyright (C) 2004-2008 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, but
12 # 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 along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 if [ ! -e "$dir/$filename" ]; then
550 iconwidth=$($IDENTIFYCOMMAND -format "%wx%h" "$dir/$filename")
551 iconheight=${iconwidth//*x}
552 iconwidth=${iconwidth//x*}
553 # are the dimensions correctish?
555 if [ ! -z $wantedheight ]; then
556 if [ $wantedwidth -ne $iconwidth ] && [ $wantedheight -ne $iconheight ]; then
559 # make sure that both of the dimensions are smaller than they
561 if [ $iconwidth -gt $wantedwidth ] || [ $iconheight -gt $wantedheight ]; then
566 if [ $wantedwidth -ne $iconwidth ]; then
573 function generate_resized_images() {
574 $ECHOCOMMAND "Generating $2"
577 if [ $width != $height ]; then
583 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
584 imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS |
585 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} |
586 while read filename; do
587 filename=${filename//\"/\\\"}
588 imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
589 if [ ! -r "$2/$filename" ] ||
590 [ "$filename" -nt "$2/$filename" ] ||
591 [ $imagesizeright -ne 1 ]; then
592 $ECHOCOMMAND $filename
597 if [ $totalimages -eq 0 ]; then
598 # might as well exit at this point - there are no pictures
599 $ECHOCOMMAND "No images for the gallery - exiting."
602 # check if the directory exists and create it other wise
608 $ECHOCOMMAND "Can't write to $2 directory, exiting"
613 if [ $totalimages -eq $imagestoupdate ]; then
614 $ECHOCOMMAND "Regenerating all $2"
615 elif [ $imagestoupdate -eq 0 ]; then
616 $ECHOCOMMAND "No Updated $2, not regenerating"
619 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
622 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
623 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
624 while read filename; do
625 tempfilename=${filename//\"/\\\"/};
626 imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
627 if [ ! -r "$2/$tempfilename" ] \
628 || [ "$tempfilename" -nt "$2/$tempfilename" ] \
629 || [ $imagesizeright -ne 1 ]; then
630 $ECHOCOMMAND -n $filename
631 $ECHOCOMMAND -n -e "\000"
634 $XARGSCOMMAND -0 --verbose --max-procs=4 -I {} $CONVERTTOOL -resize $1 '{}' $2/'{}' 2>&1 | \
635 while read throwout; do
636 $ECHOCOMMAND done: $currentimage/$imagestoupdate images
637 currentimage=$((currentimage+1))
639 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
640 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
643 function generate_thumbs() {
644 if [ ! -z $HEIGHT ]; then
645 generate_resized_images "${WIDTH}x${HEIGHT}" $ICONSDIRECTORY
647 generate_resized_images $WIDTH $ICONSDIRECTORY
651 function generate_medium() {
652 if [ ! -z $MEDIUMHEIGHT ]; then
653 generate_resized_images "${MEDIUMWIDTH}x${MEDIUMHEIGHT}" $MEDIUMDIRECTORY
655 generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
659 function generate_pages() {
660 $ECHOCOMMAND "Generating Pages"
680 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
684 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
685 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
687 while read imagefilename; do
688 previousimage=$currentimage
689 currentimage=$nextimage
690 nextimage=${imagefilename#./}
693 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
694 if [[ $addlinks == 1 ]]; then
695 addlink=${currentimage}.html
697 addlink=${currentimage}${extra}.html
700 addlink=${currentimage}
703 previouspage=$currentpage
704 currentpage=$nextpage
705 if [[ $addlinks == 1 ]]; then
706 nextpage=${nextimage}${extra}.html
708 nextpage=${nextimage}.html
711 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
712 filename=../${extradir}${currentimage}
714 filename=${extradir}${currentimage}
716 if [ "x$currentpage" != "x" ]; then
717 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
719 $ECHOCOMMAND $nextimage
725 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
726 if [[ $addlinks == 1 ]]; then
727 addlink=${currentpage}.html
729 addlink=${currentpage}${extra}.html
732 addlink=${currentpage}
735 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
736 filename=../${extradir}${currentpage}
738 filename=${extradir}${currentpage}
740 if [[ $addlinks == 1 ]]; then
741 currentpage=${currentpage}${extra}.html
742 previouspage=${previouspage}${extra}.html
744 currentpage=${currentpage}.html
745 previouspage=${previouspage}.html
748 generate_general_page "$previouspage" "$currentpage" "" $addlink
752 function generate_medium_pages() {
753 generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
756 function generate_general_page() {
771 if [[ ! -z $4 ]]; then
775 if [ -r captions.txt ]; then
776 imagefilename=${filename##*/}
777 caption=$($GREPCOMMAND -E "^${imagefilename} " captions.txt); caption=${caption#* }
782 $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
785 if [[ $OUTPUTHTML != 0 ]]; then
790 $ECHOCOMMAND "No path given"
795 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
800 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
805 if [[ ! -d $1 ]]; then
806 $ECHOCOMMAND "$1 is not a directory"
813 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
814 $ECHOCOMMAND "$1 does not contain any images. Quitting."
819 $ECHOCOMMAND "Can't write to images directory, exiting"
823 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
824 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
828 if [ -e style.css ] && [ ! -w style.css ]; then
829 $ECHOCOMMAND "Can't write style.css, exiting"
835 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
836 if [ ! -d $PAGESDIRECTORY ]; then
837 mkdir -p $PAGESDIRECTORY
840 if [ ! -w $PAGESDIRECTORY ]; then
841 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
844 UNIXPAGESDIRECTORY=$PAGESDIRECTORY/
845 PAGESDIRECTORY=$(bpgallery_escape_url $PAGESDIRECTORY)/
850 UNIXPAGESDIRECTORY=""
853 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
855 generate_medium_pages
858 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
859 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
860 generate_pages __${MEDIUMDIRECTORY}
866 if [ -r description.txt ] ; then
867 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
874 $ECHOCOMMAND "Starting to generate page"
876 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
877 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
879 $ECHOCOMMAND "Adding Captions"
882 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
883 extra=__${MEDIUMDIRECTORY}
886 $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}
888 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
890 $ECHOCOMMAND "Finished generating the page"
891 $ECHOCOMMAND "Generating stylesheet"
894 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
895 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
899 # add a bit to the maxheight for the size of the caption
900 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
902 $BPGALLERY_STYLESHEET_FUNCTION > style.css
904 $ECHOCOMMAND "All done"
906 if [[ $OUTPUTHTML != 0 ]]; then
907 $ECHOCOMMAND "</pre>"