#!/bin/bash
# 
# Wallpaper Image Gallery - an image gallery generation script for creating
# three sizes of wallpaper
#
# Script to create a wallpaper gallery.  It takes all JPEG or PNG images in
# the present working directory, then performs an operation on them four times,
# one for each desired size.  It scales them, then adds a watermark in the lower
# right, then puts it into a specified location.  (The thumbnail does not have
# the watermark added, just the three wallpaper-sized images.)  Then HTML code is
# output to a file for the image gallery.
#
# 20060219 Ed Holden, version 1.0
# 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
 location=/var/www/images/wallpaper
 # Set HTML code output for location of large-scale images
 locationhtml='<?php echo $relpath ?>/images/wallpaper'
 # Set thumbnail wide dimension in pixels
 dimthumb=140
 # Set size 1 wide dimension in pixels
 dim1=1024
 # Set size 1 wide dimension in pixels
 dim2=1280
 # Set size 1 wide dimension in pixels
 dim3=1400
 # 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=wallpapergallery.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
# Set variables for dimensions - requires identify command
    echo " Creating three images plus a thumbnail for $i ..."
      # All images should be landscape images, and we convert them
    noextension=`echo $i | sed "s:.$format::g"`
    filenamethumb="$noextension-thumb.$format"
    filename1="$noextension-1.$format"
    filename2="$noextension-2.$format"
    filename3="$noextension-3.$format"

    convert -quality 95 -geometry $dimthumb "$i" "$location/$filenamethumb"
    convert -quality 100 -geometry $dim1 "$i" temp.png
     composite -quality 95 -compose atop -gravity southeast $watermark temp.png "$location/$filename1"
    convert -quality 100 -geometry $dim2 "$i" temp.png
     composite -quality 95 -compose atop -gravity southeast $watermark temp.png "$location/$filename2"
    convert -quality 100 -geometry $dim3 "$i" temp.png
     composite -quality 95 -compose atop -gravity southeast $watermark temp.png "$location/$filename3"
    rm temp.png

# Create DIV for the image
    echo "Creating HTML code for $i in output file ..."

    echo "  <table class=\"gallerythumb\"><tr><td>" >> $outfile
    echo "    <img border='0' src=\"$locationhtml/$filenamethumb\" alt=\"$noextension\" title=\"$noextension\">" >> $outfile
    echo "    <p><strong>$noextension</strong><br>" >> $outfile
    echo -n "      <a href=\"$locationhtml/$filename1\">1024x768</a> | " >> $outfile
    echo -n "      <a href=\"$locationhtml/$filename2\">1280x1024</a> | " >> $outfile
    echo -n "      <a href=\"$locationhtml/$filename3\">1400x1050</a>" >> $outfile
    echo -n "</p>" >> $outfile
    echo "  </td></tr></table>" >> $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 ""

