#!/bin/bash
# 
# Web Image Gallery - an image gallery generation script
#
# Script to create a generic image gallery.  It take all JPEG or PNG images in
# the present working directory, determine their layout (portrait or landscape),
# and resizes them twice - once to a large dimension and one to a thumbnail
# dimension. Both can be specified.  Then it puts each into a specified
# location (this must be different for thumbnails and large images because the
# filenames are not changed, so if the locations match the thumbs will overwrite
# the large images).  Then HTML code is output to a file for the image gallery.
# The image locations must be configured separately from the output loctions
# because the HTML code must inevitably differ.  However this is good because
# it allows you to create dynamic code (e.g., PHP) inthe file paths.
#
# 20060421 Ed Holden, version 1.4
# Permission is granted to use this software under the GNU General Public
# License version 2 or later.

# User-defined options:

 # Set large-scale image dimension in pixels
 dimlarge=800
 # Set output location for large-scale images
 loclarge=/var/www/images/photos
 # Set HTML code output for location of large-scale images
 loclargehtml='<?php echo $relpath ?>/images/photos'
 # Set thumbnail dimension in pixels
 dimthumb=140
 # Set output location for thumbnails
 locthumb=/var/www/images/photos/thumbs
 # Set HTML code output for location of thumbnails
 locthumbhtml='<?php echo $relpath?>/images/photos/thumbs'
 # Image format to convert (not all are supported by ImageMagik convert)
 format="jpg"
 # A watermark image for the lower right corner, eg copyright
 watermark="copyright.png"
 # Set output filename
 outfile=gallery.php

# Change delimiter to from space to line break for filenames with spaces
OLDIFS=$IFS
IFS="
"

touch $outfile

echo ""
echo " Web Image Gallery Script running"
echo ""

# Operate on each image in the present working directory
list=`ls -1 *$format`
for i in $list
  do
    # Create extensionless filename and then cut the date string and starting space for captions
    noextension=`echo $i | sed "s:.$format::g"`
    nodate=`echo "$noextension" | cut -d- -f3`
    title=`echo $nodate | sed 's:^ ::g'`
    # Set variables for dimensions - requires identify command
    echo " Creating images and thumbnails for $i ..."
    width=`identify -format "%w" $i`
    height=`identify -format "%h" $i`
    if [ $width -gt $height ]; then
      # Convert landscape images to specified widths
      convert -quality 100 -geometry $dimlarge "$i" tmp.png
      convert -geometry $dimthumb "$i" "$locthumb/$i"
    # Convert portrait images to specified heights
    else
      convert -quality 100 -geometry x$dimlarge "$i" tmp.png
      convert -geometry x$dimthumb "$i" "$locthumb/$i"
    fi

    # Create composite using watermark
    composite -quality 95 -compose atop -gravity southeast $watermark tmp.png "$loclarge/$i"
    rm tmp.png

    # Create DIV for the image
    echo "Creating HTML code for $i in output file ..."
    echo "<div class=\"photo\">" >> $outfile
    echo -n "  <a href=\"$loclargehtml/$i\"><img border='0' src=\"$locthumbhtml/$i\"" >> $outfile
      # Process image title for TITLE and ALT tags by removing the .png or .jpg
      echo " alt=\"$title\" title=\"$title\"></a>" >> $outfile
    echo "  <div>" >> $outfile
    echo "    $title." >> $outfile
    echo "  </div>" >> $outfile
    echo "  <div class=\"spacer\"></div>" >> $outfile
    echo "</div>" >> $outfile
    echo "" >> $outfile
  done

# Restore original IFS variable
IFS=$OLDIFS

echo ""
echo "All images resized, moved and added to the gallery output file.  The original images are still in this directory."
echo ""

