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 set the width of images in the medium size pages
506 set the theme to use (described below)
509 set an extra location to look for themes
512 sets the script output to be wrapped in a <pre> block
514 GENERATEPAGESFORMEDIUMSIZE
515 generate medium sized images and pages
517 GENERATEPAGESFORFULLSIZE
518 decide wether to generate pages for the full size images or not
521 name of the index page (e.g. index.html)
524 TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
534 function check_dimensions() {
539 iconwidth=$($IDENTIFYCOMMAND -format "%wx%h" "$dir/$filename")
540 iconheight=${iconwidth//*x}
541 iconwidth=${iconwidth//x*}
542 # are the dimensions correctish?
544 if [ ! -z $wantedheight ]; then
545 if [ $wantedwidth -ne $iconwidth ] && [ $wantedheight -ne $iconheight ]; then
548 # make sure that both of the dimensions are smaller than they
550 if [ $iconwidth -gt $wantedwidth ] || [ $iconheight -gt $wantedheight ]; then
555 if [ $wantedwidth -ne $iconwidth ]; then
562 function generate_resized_images() {
563 $ECHOCOMMAND "Generating $2"
566 if [ $width != $height ]; then
572 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
573 imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS |
574 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} |
575 while read filename; do
576 filename=${filename//\"/\\\"}
577 imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
578 if [ ! -r "$2/$filename" ] ||
579 [ "$filename" -nt "$2/$filename" ] ||
580 [ $imagesizeright -ne 1 ]; then
581 $ECHOCOMMAND $filename
586 if [ $totalimages -eq 0 ]; then
587 # might as well exit at this point - there are no pictures
588 $ECHOCOMMAND "No images for the gallery - exiting."
591 # check if the directory exists and create it other wise
597 $ECHOCOMMAND "Can't write to $2 directory, exiting"
602 if [ $totalimages -eq $imagestoupdate ]; then
603 $ECHOCOMMAND "Regenerating all $2"
604 elif [ $imagestoupdate -eq 0 ]; then
605 $ECHOCOMMAND "No Updated $2, not regenerating"
608 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
611 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
612 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
613 while read filename; do
614 tempfilename=${filename//\"/\\\"/};
615 imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
616 if [ ! -r "$2/$tempfilename" ] \
617 || [ "$tempfilename" -nt "$2/$tempfilename" ] \
618 || [ $imagesizeright -ne 1 ]; then
619 $ECHOCOMMAND -n $filename
620 $ECHOCOMMAND -n -e "\000"
623 $XARGSCOMMAND -0 --verbose --max-procs=4 -I {} $CONVERTTOOL -resize $1 '{}' $2/'{}' 2>&1 | \
624 while read throwout; do
625 $ECHOCOMMAND done: $currentimage/$imagestoupdate images
626 currentimage=$((currentimage+1))
628 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
629 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
632 function generate_thumbs() {
633 if [ ! -z $HEIGHT ]; then
634 generate_resized_images "${WIDTH}x${HEIGHT}" $ICONSDIRECTORY
636 generate_resized_images $WIDTH $ICONSDIRECTORY
640 function generate_medium() {
641 if [ ! -z $MEDIUMHEIGHT ]; then
642 generate_resized_images "${MEDIUMWIDTH}x${MEDIUMHEIGHT}" $MEDIUMDIRECTORY
644 generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
648 function generate_pages() {
649 $ECHOCOMMAND "Generating Pages"
669 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
673 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
674 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
676 while read imagefilename; do
677 previousimage=$currentimage
678 currentimage=$nextimage
679 nextimage=${imagefilename#./}
682 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
683 if [[ $addlinks == 1 ]]; then
684 addlink=${currentimage}.html
686 addlink=${currentimage}${extra}.html
689 addlink=${currentimage}
692 previouspage=$currentpage
693 currentpage=$nextpage
694 if [[ $addlinks == 1 ]]; then
695 nextpage=${nextimage}${extra}.html
697 nextpage=${nextimage}.html
700 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
701 filename=../${extradir}${currentimage}
703 filename=${extradir}${currentimage}
705 if [ "x$currentpage" != "x" ]; then
706 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
708 $ECHOCOMMAND $nextimage
714 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
715 if [[ $addlinks == 1 ]]; then
716 addlink=${currentpage}.html
718 addlink=${currentpage}${extra}.html
721 addlink=${currentpage}
724 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
725 filename=../${extradir}${currentpage}
727 filename=${extradir}${currentpage}
729 if [[ $addlinks == 1 ]]; then
730 currentpage=${currentpage}${extra}.html
731 previouspage=${previouspage}${extra}.html
733 currentpage=${currentpage}.html
734 previouspage=${previouspage}.html
737 generate_general_page "$previouspage" "$currentpage" "" $addlink
741 function generate_medium_pages() {
742 generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
745 function generate_general_page() {
760 if [[ ! -z $4 ]]; then
764 if [ -r captions.txt ]; then
765 imagefilename=${filename##*/}
766 caption=$($GREPCOMMAND -E "^${imagefilename} " captions.txt); caption=${caption#* }
771 $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
774 if [[ $OUTPUTHTML != 0 ]]; then
779 $ECHOCOMMAND "No path given"
784 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
789 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
794 if [[ ! -d $1 ]]; then
795 $ECHOCOMMAND "$1 is not a directory"
802 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
803 $ECHOCOMMAND "$1 does not contain any images. Quitting."
808 $ECHOCOMMAND "Can't write to images directory, exiting"
812 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
813 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
817 if [ -e style.css ] && [ ! -w style.css ]; then
818 $ECHOCOMMAND "Can't write style.css, exiting"
824 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
825 if [ ! -d $PAGESDIRECTORY ]; then
826 mkdir -p $PAGESDIRECTORY
829 if [ ! -w $PAGESDIRECTORY ]; then
830 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
833 UNIXPAGESDIRECTORY=$PAGESDIRECTORY/
834 PAGESDIRECTORY=$(bpgallery_escape_url $PAGESDIRECTORY)/
839 UNIXPAGESDIRECTORY=""
842 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
844 generate_medium_pages
847 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
848 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
849 generate_pages __${MEDIUMDIRECTORY}
855 if [ -r description.txt ] ; then
856 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
863 $ECHOCOMMAND "Starting to generate page"
865 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
866 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
868 $ECHOCOMMAND "Adding Captions"
871 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
872 extra=__${MEDIUMDIRECTORY}
875 $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}
877 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
879 $ECHOCOMMAND "Finished generating the page"
880 $ECHOCOMMAND "Generating stylesheet"
883 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
884 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
888 # add a bit to the maxheight for the size of the caption
889 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
891 $BPGALLERY_STYLESHEET_FUNCTION > style.css
893 $ECHOCOMMAND "All done"
895 if [[ $OUTPUTHTML != 0 ]]; then
896 $ECHOCOMMAND "</pre>"