mkpicindex/mkpicindex-static.sh

184 lines
5.6 KiB
Bash
Raw Normal View History

2019-10-25 16:57:27 +02:00
#!/bin/sh
printf '%s' \
'/*!
* mkpicindex.sh - v0.1
* https://codevoid.de
* Copyright (c) 2019 Stefan Hagen
* Licensed under the ISC license.
*/
' > LICENSE
# CONFIGURE
2019-10-26 14:13:47 +02:00
TITLE="My Gallery" # browser title
WIDTH=1000 # how wide will the gallery be
ROW_HEIGHT=150 # how high will the justified rows be?
THUMB_QUALITY=83 # quality for thumbnails
THUMB_PATH="thm" # relative path to thumbnail folder
THUMB_PADDING="6" # image padding
DEBUG=$1 # debug output
# GLOBAL TMP VARIABLES
G_ROW_WIDTH=0 # combined pic width < WIDTH @ ROW_HEIGHT
G_ROW_FILES="" # pipe separated files < WIDTH
MORE=1 # trigger next loop
2019-10-25 16:57:27 +02:00
### ZE PROGAM STARTZ HERE ##############################################
# CREATE THUMBNAIL DIRECTORY
2019-10-26 14:13:47 +02:00
mkdir -p "$THUMB_PATH"
# OUTPUT HELPER
debug() { [ "$DEBUG" == "1" ] && printf '%s\n' "Debug: $1" >&2; }
console() { printf '%s\n' "$1" >&2; }
2019-10-26 16:45:50 +02:00
# CALCULATE BY ASPECT RATIO
2019-10-26 14:13:47 +02:00
get_width_by_height() {
# returns aspect ratio calculated width
2019-10-26 15:25:55 +02:00
local F="$1" # image file
local TH="$2" # target height
2019-10-26 14:13:47 +02:00
local WH="$(identify -format ' %w %h ' "$1" | awk '{ printf("%.3f %.3f",$1,$2) }')"
2019-10-26 15:25:55 +02:00
local R="$(printf "$WH" | awk -vTH=$TH '{ printf("%.0f", TH*($1/$2)) }')"
printf '%.0f' "$R"
2019-10-26 14:13:47 +02:00
debug "get_width_by_height: FILE=$F TARGET_HEIGHT=$TH FILE_WxH=$WH RET_WIDTH=$R"
}
2019-10-26 16:45:50 +02:00
# TOO MANY CONVERT PROCSSES => WAIT
2019-10-26 15:54:58 +02:00
bg_check() {
2019-10-26 16:37:23 +02:00
while [ $(pgrep convert | wc -l | awk '{ print $1 }') -gt 4 ];
2019-10-26 16:45:50 +02:00
do console "More than 4 convert threads. Waiting..."; sleep 2; done
2019-10-26 15:54:58 +02:00
}
2019-10-26 14:13:47 +02:00
# CREATE THUMBNAIL
create_thumb() {
local F="$1" # original
local W="$2" # width
local H="$3" # height
local T="${F%%.*}-$H"
2019-10-26 16:37:23 +02:00
if ! [ -f "$THUMB_PATH/$T.gif" ] && ! [ -f "$THUMB_PATH/$T.jpeg" ];
bg_check
2019-10-26 14:13:47 +02:00
then
case $(printf '%s' "${F##*.}" | tr '[:upper:]' '[:lower:]') in
2019-10-26 16:37:23 +02:00
gif) console "Creating Thumbnail: $THUMB_PATH/$T.gif"
nohup convert -quality $THUMB_QUALITY -sharpen 2x2 \
-coalesce -resize 6000x$H\> \
-deconstruct "$F" \
"$THUMB_PATH/$T.gif" >/dev/null 2>&1 &
2019-10-26 15:54:58 +02:00
printf '%s' "$THUMB_PATH/$T.gif" ;;
2019-10-26 16:37:23 +02:00
*) console "Creating Thumbnail: $THUMB_PATH/$T.jpeg"
nohup convert -quality $THUMB_QUALITY -sharpen 2x2 \
-resize 6000x$H\> "$F" \
"$THUMB_PATH/$T.jpeg" >/dev/null 2>&1 &
2019-10-26 15:54:58 +02:00
printf '%s' "$THUMB_PATH/$T.jpeg" ;;
2019-10-26 14:13:47 +02:00
esac
fi
}
2019-10-25 16:57:27 +02:00
2019-10-26 14:13:47 +02:00
# ADD IMAGE LOOP
add_image() {
2019-10-26 15:25:55 +02:00
local F="$1" # image file
2019-10-26 14:13:47 +02:00
# How wide would the image be when we rescale it to $ROW_HEIGHT?
local NW=$(get_width_by_height "$F" "$ROW_HEIGHT")
debug "add_image: FILE=$F NW=${NW}x$ROW_HEIGHT"
2019-10-25 16:57:27 +02:00
2019-10-26 14:13:47 +02:00
# We add images and their width to $G_ROW_WIDTH until $WIDTH will
# be exceeded.
if [ "$(( $G_ROW_WIDTH + $NW ))" -gt "$WIDTH" ]; then
2019-10-25 16:57:27 +02:00
2019-10-26 14:13:47 +02:00
debug "add_image: max width reached with F=$F @ $G_ROW_WIDTH"
2019-10-25 16:57:27 +02:00
2019-10-26 14:13:47 +02:00
# we're building a row now
printf "<div class=\"row\">\n";
2019-10-25 16:57:27 +02:00
2019-10-26 14:13:47 +02:00
# calculate how much we need to stretch images to fill the
# whole row.
2019-10-26 15:25:55 +02:00
local RFH=$(printf "$G_ROW_WIDTH $WIDTH $ROW_HEIGHT" \
2019-10-26 14:13:47 +02:00
| awk '{ printf("%.0f",$3*($2/$1)) }')
debug "RFH=$RFH"
2019-10-25 16:57:27 +02:00
2019-10-26 14:13:47 +02:00
# loop through the images in this row and recalculate
# them with their new, real height.
local IFS='|'; for RF in $G_ROW_FILES;
do
local RFW=$(($(get_width_by_height "$RF" "$RFH") - 2*$THUMB_PADDING))
debug "add_image: adding file: F=$RF with W=$RFW H=$RFH"
2019-10-25 16:57:27 +02:00
2019-10-26 14:13:47 +02:00
local T=$(create_thumb "$RF" "$RFW" "$RFH")
debug "add_image: created thumbnail $T"
2019-10-25 16:57:27 +02:00
2019-10-26 14:13:47 +02:00
# output HTML for image
console "Adding Image: $RF"
2019-10-26 15:25:55 +02:00
printf ' <div class="image">\n'
printf ' <a href="'$RF'">\n'
printf ' <img width="'$RFW'" height="'$RFH'" src="'$T'">'
printf ' </a>\n'
printf ' </div>\n'
2019-10-26 14:13:47 +02:00
done
# we're done with this row now.
printf "</div>\n";
2019-10-25 16:57:27 +02:00
# set leftover file as for next iteration
2019-10-26 14:13:47 +02:00
G_ROW_WIDTH="$NW"
G_ROW_FILES="$F|"
2019-10-25 16:57:27 +02:00
else
# add more items...
2019-10-26 14:13:47 +02:00
debug "add_image: width has not been reached, continue loop."
2019-10-26 15:25:55 +02:00
G_ROW_WIDTH="$(( $G_ROW_WIDTH + $NW ))"
2019-10-26 14:13:47 +02:00
G_ROW_FILES="$F|$G_ROW_FILES"
2019-10-25 16:57:27 +02:00
fi
}
2019-10-26 14:13:47 +02:00
# HEADER
printf '%s\n' \
'<html>
<head>
<meta name="viewport" content="width=device-width">
<title>My Gallery</title>
<style>
html {
background: black;
color: orange;
}
.base {
margin-left: auto;
margin-right: auto;
width: min-content;
}
.row {
display: block;
float: clear;
width: max-content;
margin-left: auto;
margin-right: auto;
white-space: nowrap;
}
.image {
float: left;
width: fit-content;
height: fit-content;
padding: '"$THUMB_PADDING"';
}
</style>
</head>
<body>
<div class="base">
'
### MAIN LOOP ##########################################################
for F in *.*;
2019-10-25 16:57:27 +02:00
do
2019-10-26 14:13:47 +02:00
if [ -f "$F" ];
2019-10-25 16:57:27 +02:00
then
2019-10-26 15:25:55 +02:00
case "$(printf '%s' ${F##*.} | tr '[:upper:]' '[:lower:]')" in
2019-10-26 14:13:47 +02:00
jpg|jpeg|png|gif) add_image "$F" ;;
*) console "Ignoring: $F" ;;
2019-10-25 16:57:27 +02:00
esac
fi
done
2019-10-26 14:13:47 +02:00
### MAIN LOOP END ######################################################
2019-10-25 16:57:27 +02:00
2019-10-26 14:13:47 +02:00
# FOOTER
printf '%s\n' \
' </div>
</body>
</html>'