Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Poor man's html image gallery

For a personal website for which I hand-roll the html code, I wrote a small shell image link+thumbnail generator script. I shouldn't be proud, but I am.

#!/bin/sh
# Startdate: 2020-11-27 15:09
INDIR=/mnt/public/www/images
DIRPREFIX="images"
THUMBNAIL_SIZE=300
USE_THUMBNAILS=1
if test -n "${USE_THUMBNAILS}" && test "${USE_THUMBNAILS}" = 1 ;
then
   mkdir -p "${INDIR}/.thumbnails"
fi
cd "${INDIR}"
tfs="$( find "${INDIR}" -mindepth 1 -maxdepth 1 ! -type d ! -name '.*.swp' ! -name '*.sh' ! -name '*.html' -printf '%T@ %f\n' | sort -n | awk '{print $NF}' )"
for word in ${tfs} ;
do
   tf="${DIRPREFIX}/${word}"
   tf_thumb_web="${tf}"
   if test -n "${USE_THUMBNAILS}" && test "${USE_THUMBNAILS}" = 1 ;
   then
      tf_in="${INDIR}/${word}"
      tf_thumb="${INDIR}/.thumbnails/${word%%.???}.jpg"
      tf_thumb_web="${DIRPREFIX}/.thumbnails/${word%%.???}.jpg"
      convert -filter Lanczos -resize "${THUMBNAIL_SIZE}x${THUMBNAIL_SIZE}" "${tf_in}" "${tf_thumb}"
   fi
   echo "<a href=\"${tf}\"><img src=\"${tf_thumb_web}\" class=\"thumb\"></a>"
done

The find | sort | awk line is my preferred method for sorting by timestamp. It is not safe for files with spaces in the name, but who does that anyways?! (Ahem. [Well, not here obviously!]). Of course there are ways to make that happen, but it would be even less clean looking, and avoiding spaces is easy enough.

So like all good unix scripts, this just prints to stdout. I then copy-paste the contents and update my html file.

I haven't done extensive testing with non-square images. I'm pretty sure most images on my gallery are non-square, but everything seems to line up adequately.

And what gives away the fact that I'm a vim user?

And of course, some applicable css:

.thumb {
   max-width: 300px;
   max-height: 300px;
}

Comments