# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . #!/bin/bash # Author: Nils Knieling - https://github.com/Cyclenerd/gallery_shell ( source code ) # used and updated by anarcho-copy.org ######################################################################################### #### Configuration Section ######################################################################################### ##### please before run the program enter this code: "apt-get install imagemagick jhead" because this shell script need the imagemick and jhead ##### height_small=187 height_large=768 quality=85 thumbdir="__thumbs" htmlfile="index.html" title="Photography Gallery" footer='Created with anarcho-copy gallery generator (BASH)' # Use convert from ImageMagick convert="convert" # Use JHead for EXIF Information exif="jhead" # Bootstrap (currently v3.4.1) stylesheet="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" downloadicon='' movieicon='' homeicon='return to archive' # Debugging output # true=enable, false=disable debug=true ######################################################################################### #### End Configuration Section ######################################################################################### me=$(basename "$0") datetime=$(date -u "+%Y-%m-%d %H:%M:%S") datetime+=" UTC" function usage { returnCode="$1" echo -e "Usage: $me [-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 "$returnCode" } 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 myfilesize=$(stat -c %s "$1" | awk '{$1/=1000000;printf "%.2fMB\n",$1}') else # BSD myfilesize=$(stat -f %z "$1" | awk '{$1/=1000000;printf "%.2fMB\n",$1}') fi echo "$myfilesize" } 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 "- $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 debugOutput "$htmlfile" cat > "$htmlfile" << 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 echo '
' >> "$htmlfile" ## Generate Images numfiles=0 for filename in *.[jJ][pP][gG]; do filelist[$numfiles]=$filename (( numfiles++ )) for res in ${heights[*]}; do if [[ ! -s $thumbdir/$res/$filename ]]; then debugOutput "$thumbdir/$res/$filename" $convert -auto-orient -strip -quality $quality -resize x$res "$filename" "$thumbdir/$res/$filename" fi done 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=$(getFileSize "$filename") debugOutput "$imagehtmlfile" cat > "$imagehtmlfile" << EOF $filename

$homeicon / $filename

EOF # Pager echo '

' >> "$imagehtmlfile" [[ $prev ]] && echo ' |' >> "$imagehtmlfile" [[ $next ]] && echo '' >> "$imagehtmlfile" echo '

' >> "$imagehtmlfile" cat >> "$imagehtmlfile" << EOF

Download Original File ($filesize)

EOF # EXIF if [[ $exifinfo ]]; then cat >> "$imagehtmlfile" << EOF
$exifinfo
EOF fi # Footer cat >> "$imagehtmlfile" << 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 >> "$htmlfile" << 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 >> "$htmlfile" << EOF $movieicon $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 >> "$htmlfile" << EOF $movieicon $filename ($filesize) EOF done fi echo '
' >> "$htmlfile" fi ### Downloads (ZIP) if [[ $(find . -maxdepth 1 -type f -iname \*.zip | wc -l) -gt 0 ]]; then cat >> "$htmlfile" << EOF
EOF for filename in *.[zZ][iI][pP]; do filesize=$(getFileSize "$filename") cat >> "$htmlfile" << EOF $downloadicon $filename ($filesize) EOF done echo '
' >> "$htmlfile" fi ### Footer cat >> "$htmlfile" << EOF

$footer

$datetime

EOF debugOutput "= done :-)"