Cut and paste below here


#!/bin/bash

############################################################################################
#
#    resizefoririver.sh - Resize an image and remove EXIF thumbnail to fit iRiver H3xx series displays
#
#    Written by Ben Williams September 24th 2004
#    Web: http://sabaisabai.org/iRiver/
#    Email: webben@saabaisaabai.org
#
#    NOTE:  
#
#      1. By default this will backup the original image as imagename.jpg.original.  
#        Override this with the '-nobackup' parameter, or permanently change the backup/nobackup
#        lines in the script file
#
#     2. Images fill as much of the display as possible, so vertical images will by default 
#        be rotated to sit sideways.  Override this with the '-upright' parameter
#     
#     3. Somebody else on a German iRiver forum has come up with the same idea as this, however
#        the idea and this script is original work by me.
#
#     4. The display size is 220x176.  Resizing an image to "220x176" will result in an image-size of 220x165, 
#        maintaining the aspect ratio of 4:3 and without cropping the picture.
#        size of 220x165 is used.
#
#    REQUIREMENTS:
#  
#     1. 'convert' utility.  This is part of the imagemagick suite, available at http://www.imagemagick.org/
#
#     2. 'jhead' utility.  This is available at http://www.sentex.net/~mwandel/jhead/
#
#     UPDATES
#
#     10th October 2004 - changed the dimensions of the vertical upright images from 123x165 to 132x176, 
#     to fill the full height of the display.
#
#
############################################################################################

HORIZONTALHEIGHT=176
HORIZONTALWIDTH=220

VERTICALHEIGHT=176
VERTICALWIDTH=132

RESIZEPROGRAM=convert

# USAGE="Usage: resizefoririver.sh [-backup] [-upright] [--help]"
USAGE="Usage: resizefoririver.sh [-nobackup] [-upright] [-showonly] [--help]"

if [ "${1}" = "--help" ] 
then
   echo Script for resizing images to fit iRiver H3xx series displays
   echo 
   echo ${USAGE}
   #echo "   backup  : keep a copy of the original image as imagename.jpg.original"
   echo "  nobackup : replace original image with resized image"
   echo "   upright : keep vertical images upright"
   echo "  showonly : show the name of files to be resized and exit"
   echo "   --help  : show this message"
   exit 0
fi

for param in $*
do
   if [ "${param}" = "-upright" ]
   then
      upright="y"
   #elif [  "${param}" = "-backup" ]
   #then
   #   backup="y"
   elif [  "${param}" = "-nobackup" ]
   then
      nobackup="y"
   elif [  "${param}" = "-showonly" ]
   then
      showonly="y"
   else
      filename=${param}
   fi
done

if [ "${filename}" = "" ]
then
   echo $USAGE >&2
   exit 1
fi

echo Resizing "${filename}" ...

if [ "${showonly}" = "y" ]
then
   exit 0
fi

if ! [ -f ${filename} ]
then
   echo "File '${filename}' not found" >&2
   exit 1
fi

file ${filename} | grep JPEG > /dev/null 2>&1

if [ "${?}" != "0" ]
then
   echo "File '${filename}' is not a JPEG image" >&2
   exit 1
fi

resolution=`jhead $filename | grep Resolution | cut -d: -f2`
width=`echo $resolution | cut -dx -f1`
height=`echo $resolution | cut -dx -f2`
   
if [ $height -gt $width ]
then
   # Image is vertical
   
   if [ "${upright}" = "y" ]
   then
      # Keep image upright, creating spaces on the left and side of the image
      ${RESIZEPROGRAM} -size ${VERTICALWIDTH}x${VERTICALHEIGHT} $filename -resize ${VERTICALWIDTH}x${VERTICALHEIGHT} ${filename}.tmp > /dev/null
      convertresult=$?
   else
      # Use full screen space by rotating image on it's side
      ${RESIZEPROGRAM} -size ${HORIZONTALHEIGHT}x${HORIZONTALWIDTH} $filename -resize ${HORIZONTALHEIGHT}x${HORIZONTALWIDTH}  -rotate 270 ${filename}.tmp > /dev/null
      convertresult=$?
   fi
else
   # Image is horizontal
   ${RESIZEPROGRAM} -size ${HORIZONTALWIDTH}x${HORIZONTALHEIGHT} $filename -resize ${HORIZONTALWIDTH}x${HORIZONTALHEIGHT} ${filename}.tmp > /dev/null
   convertresult=$?
fi

if [ "${convertresult}" != "0" ]
then
   echo Resizing failed. >&2
   exit ${convertresult}
fi

   
# Remove the internal thumbnail if there is still one
jhead -dt ${filename}.tmp > /dev/null
jheadresult=$?

if [ "${jheadresult}" != "0" ]
then
   echo Warning: removing thumbnail failed. >&2
fi


#if [ "${backup}" = "y" ]
if [ "${nobackup}" != "y" ]
then
   mv ${filename} ${filename}.original > /dev/null 
fi

mv ${filename}.tmp ${filename} > /dev/null