#!/bin/bash # gallery.sh # Author: Nils Knieling - https://github.com/Cyclenerd/gallery_shell # Inspired by: Shapor Naghibzadeh - https://github.com/shapor/bashgal ######################################################################################### #### Configuration Section ######################################################################################### height_small=187 height_large=768 quality=85 thumbdir=__thumbs htmlfile=index.html title="Gallery" footer='Created with gallery.sh' # Use convert from ImageMagick convert="convert" # Use JHead for EXIF Information exif="jhead" # Bootstrap (currently v3.3.7) # Latest compiled and minified CSS stylesheet="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" downloadicon='' movieicon='' homeicon='' function debug { return 0 # 0=enable, 1=disable debugging output } ######################################################################################### #### End Configuration Section ######################################################################################### me=$(basename "$0") datetime=$(date -u "+%Y-%m-%d %H:%M:%S") datetime+=" UTC" function usage { echo "usage: $me [-t ] [-h]" echo " [-t <title>] sets the title (default: $title)" echo " [-h] displays help (this message)" } while getopts ":t:h" opt; do case $opt in t) title="$OPTARG" ;; h) usage exit 0 ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; esac done debug && echo "- $me : $datetime" ### Check Commands command -v $convert >/dev/null 2>&1 || { echo >&2 "!!! $convert it's not installed. Aborting."; exit 1; } command -v $exif >/dev/null 2>&1 || { echo >&2 "!!! $exif it's not installed. Aborting."; exit 1; } ### Create Folders [[ -d $thumbdir ]] || mkdir $thumbdir || exit 2 heights[0]=$height_small heights[1]=$height_large for res in ${heights[*]}; do [[ -d $thumbdir/$res ]] || mkdir -p $thumbdir/$res || exit 3 done #### Create Startpage debug && echo "+" $htmlfile cat > "$htmlfile" << EOF <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8"> <title>$title
EOF ### Photos (JPG) if [[ $(find . -type f -name \*.jpg -maxdepth 1 | wc -l) -gt 0 ]]; then echo '
' >> "$htmlfile" ## Generate Images numfiles=0 for filename in *.[jJ][pP][gG]; do debug && echo -n "+ $filename: " filelist[$numfiles]=$filename let numfiles++ for res in ${heights[*]}; do debug && echo -n "$thumbdir/$res " if [[ ! -s $thumbdir/$res/$filename ]]; then $convert -auto-orient -strip -quality $quality -resize x$res "$filename" "$thumbdir/$res/$filename" fi done debug && echo cat >> "$htmlfile" << EOF

EOF [[ $(( $numfiles % 4 )) -eq 0 ]] && echo '
' >> "$htmlfile" done echo '
' >> "$htmlfile" ## Generate the HTML Files for Images in thumbdir file=0 while [[ $file -lt $numfiles ]]; do filename=${filelist[$file]} prev= next= [[ $file -ne 0 ]] && prev=${filelist[$((file - 1))]} [[ $file -ne $((numfiles - 1)) ]] && next=${filelist[$((file + 1))]} imagehtmlfile=$thumbdir/$filename.html exifinfo=$($exif "$filename") filesize=$(wc -c < "$filename" | awk '{$1/=1000000;printf "%.2fMB\n",$1}') cat > "$imagehtmlfile" << EOF $filename
EOF # Pager echo '
' >> "$imagehtmlfile" cat >> "$imagehtmlfile" << EOF

EOF # EXIF if [[ $exifinfo ]]; then cat >> "$imagehtmlfile" << EOF
$exifinfo
EOF fi # Footer cat >> "$imagehtmlfile" << EOF
EOF let file++ done fi ### Movies (MOV or MP4) if [[ $(find . -type f -name \*.mov -o -name '*.mp4' -maxdepth 1 | wc -l) -gt 0 ]]; then cat >> "$htmlfile" << EOF
EOF if [[ $(find . -type f -name \*.mov -maxdepth 1 | wc -l) -gt 0 ]]; then for filename in *.[mM][oO][vV]; do filesize=$(wc -c < "$filename" | awk '{$1/=1000000;printf "%.2fMB\n",$1}') cat >> "$htmlfile" << EOF $movieicon $filename ($filesize) EOF done fi if [[ $(find . -type f -name \*.mp4 -maxdepth 1 | wc -l) -gt 0 ]]; then for filename in *.[mM][pP]4; do filesize=$(wc -c < "$filename" | awk '{$1/=1000000;printf "%.2fMB\n",$1}') cat >> "$htmlfile" << EOF $movieicon $filename ($filesize) EOF done fi echo '
' >> "$htmlfile" fi ### Downloads (ZIP) if [[ $(find . -type f -name \*.zip -maxdepth 1 | wc -l) -gt 0 ]]; then cat >> "$htmlfile" << EOF
EOF for filename in *.[zZ][iI][pP]; do filesize=$(wc -c < "$filename" | awk '{$1/=1000000;printf "%.2fMB\n",$1}') cat >> "$htmlfile" << EOF $downloadicon $filename ($filesize) EOF done echo '
' >> "$htmlfile" fi ### Footer cat >> "$htmlfile" << EOF
EOF debug && echo "= done :-)"