#!/bin/bash ######################################################################################### #### Configuration Section ######################################################################################### HEIGHT_SMALL=406 HEIGHT_LARGE=768 QUALITY=85 THUMBDIR="thumbnails" INDEX_HTML_FILE="index.html" TITLE="Gallery" # FOOTER='Created with galgen' # Use convert from ImageMagick CONVERT_COMMAND="magick" # Use JHead for EXIF Information EXIF_COMMAND="jhead" # Bootstrap 5 CSS="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" # Debugging output # true=enable, false=disable DEBUG=true ######################################################################################### #### End Configuration Section ######################################################################################### SCRIPT_NAME=$(basename "$0") DATETIME=$(date -u "+%Y-%m-%d %H:%M:%S") DATETIME+=" UTC" function usage { RETURN_CODE="$1" echo -e "Usage: $SCRIPT_NAME [-t ] [-d <thumbdir>] [-h]: [-t <title>]\\t sets the title (default: $TITLE) [-d <thumbdir>]\\t sets the thumbdir (default: $THUMBDIR) [-h]\\t\\t displays help (this message)" exit "$RETURN_CODE" } function debugOutput(){ if [[ "$DEBUG" == true ]]; then echo "$1" # if debug variable is true, echo whatever's passed to the function fi } function getFileSize(){ # Be aware that BSD stat doesn't support --version and -c if stat --version &>/dev/null; then # GNU FILE_SIZE=$(stat -c %s "$1" | awk '{$1/=1000000;printf "%.2fMB\n",$1}') else # BSD FILE_SIZE=$(stat -f %z "$1" | awk '{$1/=1000000;printf "%.2fMB\n",$1}') fi echo "$FILE_SIZE" } while getopts ":t:d:h" opt; do case $opt in t) TITLE="$OPTARG" ;; d) THUMBDIR="$OPTARG" ;; h) usage 0 ;; *) echo "Invalid option: -$OPTARG" usage 1 ;; esac done debugOutput "- $SCRIPT_NAME : $DATETIME" ### Check Commands command -v $CONVERT_COMMAND >/dev/null 2>&1 || { echo >&2 "!!! $CONVERT_COMMAND it's not installed. Aborting."; exit 1; } command -v $EXIF_COMMAND >/dev/null 2>&1 || { echo >&2 "!!! $EXIF_COMMAND 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 debugOutput "$INDEX_HTML_FILE" cat > "$INDEX_HTML_FILE" << _EOF <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8"> <title>$TITLE
_EOF ### Photos (JPG) if [[ $(find . -maxdepth 1 -type f -iname \*.jpg | wc -l) -gt 0 ]]; then ROWS='3' echo '
' >> "$INDEX_HTML_FILE" ## Generate Images NUM_FILES=0 for FILENAME in $(ls -r *.[jJ][pP][gG]); do FILELIST[$NUM_FILES]=$FILENAME (( NUM_FILES++ )) debugOutput "$THUMBDIR/$HEIGHT_SMALL/$FILENAME" $CONVERT_COMMAND "$FILENAME" -auto-orient -strip -quality $QUALITY -resize "$HEIGHT_SMALL^>" -gravity center -crop "$HEIGHT_SMALL"x"$HEIGHT_SMALL"+0+0 "$THUMBDIR/$HEIGHT_SMALL/$FILENAME" debugOutput "$THUMBDIR/$HEIGHT_LARGE/$FILENAME" $CONVERT_COMMAND "$FILENAME" -auto-orient -strip -quality $QUALITY -resize x$HEIGHT_LARGE "$THUMBDIR/$HEIGHT_LARGE/$FILENAME" cat >> "$INDEX_HTML_FILE" << _EOF
thumbnail: $FILENAME
_EOF done echo '
' >> "$INDEX_HTML_FILE" ## Generate the HTML Files for Images in thumbdir FILE=0 while [[ $FILE -lt $NUM_FILES ]]; do FILENAME=${FILELIST[$FILE]} PREV="" NEXT="" [[ $FILE -ne 0 ]] && PREV=${FILELIST[$((FILE - 1))]} [[ $FILE -ne $((NUM_FILES - 1)) ]] && NEXT=${FILELIST[$((FILE + 1))]} IMAGE_HTML_FILE="$THUMBDIR/$FILENAME.html" EXIF_INFO=$($EXIF_COMMAND "$FILENAME") FILESIZE=$(getFileSize "$FILENAME") debugOutput "$IMAGE_HTML_FILE" cat > "$IMAGE_HTML_FILE" << _EOF $FILENAME
_EOF # Pager echo '
' >> "$IMAGE_HTML_FILE" if [[ $PREV ]]; then echo '« Previous' >> "$IMAGE_HTML_FILE" else echo '« Previous' >> "$IMAGE_HTML_FILE" fi cat >> "$IMAGE_HTML_FILE" << _EOF

$FILENAME

_EOF if [[ $NEXT ]]; then echo 'Next »' >> "$IMAGE_HTML_FILE" else echo 'Next »' >> "$IMAGE_HTML_FILE" fi echo '
' >> "$IMAGE_HTML_FILE" cat >> "$IMAGE_HTML_FILE" << _EOF

Image: $FILENAME

_EOF # EXIF if [[ $EXIF_INFO ]]; then cat >> "$IMAGE_HTML_FILE" << _EOF
$EXIF_INFO
_EOF fi # Footer # cat >> "$IMAGE_HTML_FILE" << _EOF #
#
# # # # _EOF cat >> "$IMAGE_HTML_FILE" << _EOF
_EOF (( FILE++ )) done fi ### Movies (MOV or MP4) if [[ $(find . -maxdepth 1 -type f -iname \*.mov -o -iname '*.mp4' | wc -l) -gt 0 ]]; then cat >> "$INDEX_HTML_FILE" << _EOF
_EOF if [[ $(find . -maxdepth 1 -type f -iname \*.mov | wc -l) -gt 0 ]]; then for FILENAME in *.[mM][oO][vV]; do FILESIZE=$(getFileSize "$FILENAME") cat >> "$INDEX_HTML_FILE" << _EOF $FILENAME ($FILESIZE) _EOF done fi if [[ $(find . -maxdepth 1 -type f -iname \*.mp4 | wc -l) -gt 0 ]]; then for FILENAME in *.[mM][pP]4; do FILESIZE=$(getFileSize "$FILENAME") cat >> "$INDEX_HTML_FILE" << _EOF $FILENAME ($FILESIZE) _EOF done fi echo '
' >> "$INDEX_HTML_FILE" fi ### Downloads (ZIP) if [[ $(find . -maxdepth 1 -type f -iname \*.zip | wc -l) -gt 0 ]]; then cat >> "$INDEX_HTML_FILE" << _EOF
_EOF for FILENAME in *.[zZ][iI][pP]; do FILESIZE=$(getFileSize "$FILENAME") cat >> "$INDEX_HTML_FILE" << _EOF $FILENAME ($FILESIZE) _EOF done echo '
' >> "$INDEX_HTML_FILE" fi ### Footer # cat >> "$INDEX_HTML_FILE" << _EOF # #
# # # # _EOF cat >> "$INDEX_HTML_FILE" << _EOF _EOF debugOutput "= done"