website/scripts/mkphotos.sh

99 lines
2.7 KiB
Bash

#!/bin/sh -e
## settings
DIR="$(basename "$PWD")"
TITLE="$(basename "$PWD" |sed 's/unlisted-//g' | cut -d"_" -f2- | tr '-' ' ')"
PROCS="$(sysctl -n hw.ncpuonline)"
mkdir -p thm mid
echo "<h1>$TITLE</h1>"
[ -f index.txt ] && cat index.txt
echo "<p>"
echo " <table class=\"photo-table\">"
echo " <tr>"
_count=0
find * -maxdepth 0 -mindepth 0 -type f \( -iname "*.jpg" \
-o -iname "*.jpeg" \
-o -iname "*.png" \
-o -iname "*.gif" \
\) \
| sort -r | while read _file
do
_file_thm="thm/${_file%%.*}.jpg"
_file_mid="mid/${_file%%.*}.jpg"
_file_html="${_file%%.*}.html"
_file_descr="${_file%%.*}.txt"
if [ ! -f "${_file_thm}" ]
then
convert -quality 73 \
-sharpen 2x2 \
-auto-orient \
-define jpeg:size=440x440 \
"$_file" \
-thumbnail 220x220^ \
-gravity center \
-extent 220x220 \
"${_file_thm}" \
>/dev/null 2>&1 &
fi
if [ ! -f "${_file_mid}" ]
then
convert -quality 83 \
-sharpen 2x2 \
-auto-orient \
-define jpeg:size=1440x2400 \
"$_file" \
-thumbnail 720x1600 \
"${_file_mid}" \
>/dev/null 2>&1 &
fi
if [ ! -f "${_file_html}" ]
then
echo "<h1>$TITLE</h1>" > "${_file_html}"
echo "<p>File: ${_file} (<a href=\"/photos/$DIR/${_file}\" target=_blank>Open Original</a>)</p>" >> "${_file_html}"
echo "<p><img class=\"photo-mid\" alt=\"Preview Image ${_count}\" src=\"/photos/$DIR/${_file_mid}\"></p>" >> "${_file_html}"
[ -f "${_file_descr}" ] && cat "${_file_descr}" >> "${_file_html}"
echo "<p><a href=index.html>Back to Index</a></p>" >> "${_file_html}"
fi
[ $_count -ne 0 ] \
&& [ $(( _count % 3 )) -eq 0 ] \
&& echo " </tr><tr>"
echo " <td class=\"photo-td\" ><a href=\"/photos/$DIR/${_file_html}\"><img alt=\"Thumbnail Image ${_count}\" class=\"photo-img\" src=\"${_file_thm}\"></a></td>"
# increase file counter
_count=$(( _count + 1))
while [ $(pgrep convert | wc -l) -gt $PROCS ]
do sleep 0.3; done
done
# fill missing columns
for i in $(( _count % 3 ))
do
echo " <td class=\"photo-td\" >&nbsp;</td>"
done
echo " </tr>"
echo " </table>"
echo "</p>"
echo "<p><a href=../index.html>Back to the gallery index</a></p>"
echo "section: photos" > dir.meta
wait
# it looks like wait doesn't catch processes spawned
# in a subshell. So we check explicitely for running
# convert processes. XXX
while [ $(pgrep convert | wc -l) -ge 1 ]
do sleep 1; done