#!/bin/bash # # Script Name: photo_album.sh # Pete Nesbitt, August 2005 # script to create a photo album from a dir of images. # this will be a basic script to work on a single directory of jpg images. # requires ImageMagic possibly using ssh (scp) to move files to server. # general IM command line to create thumbnails: # mogrify -format gif -size 120x90 -thumbnail 120x90 *.jpg;mv *.gif thumbnails/ IMG_TYPE="JPEG" IMG_EXT="jpg" IMG_EXT_2="JPG" # some functions to save ttyping... error_mesg_perm () { echo " Check the permissions, or choose a different location, and try again." } ####### error_1 () { echo " ERROR: There is a \"${ERR_TARGET}\" directory, but you can not write to it." error_mesg_perm echo "" exit 1 } ###### error_2 () { echo " ERROR: There is a file named \"${ERR_TARGET}\", but it does not appear to be a directory." echo " Remove or rename the \"${ERR_TARGET}\" file, or choose a different location, and try again." echo "" exit 1 } ###### error_3 () { echo " ERROR: Could not create the \"${ERR_TARGET}\" directory." error_mesg_perm echo "" exit 1 } ###### error_4 () { echo " ERROR: Could not access \"${SRC_DIR}\"" error_mesg_perm echo "" exit 1 } ###### print_SCREEN() { clear echo "" echo "" echo " ###########################################" echo "" echo " PICTURE PLACEMAT " echo "" echo " Create Images and Thumbnails for the Web " echo "" echo " Copyright 2005 Pete Nesbitt " echo "" echo " ###########################################" echo "" echo "" echo "" } ###### # clean trailing slash clean_trailing_slash () # $DIR_PATH defined where function called from { if [ `echo ${DIR_PATH}|egrep "/$"` ]; then DIR_PATH="`echo ${DIR_PATH}|sed -e 's/\/$//'`" fi } ###### # start of main script sections print_SCREEN ERR_TARGET="" # we need to get the src dir SRC_DIR="" echo "" echo " Where are the source images?" echo "" printf " => Enter full path to images: [${PWD}] " read SRC_DIR if [ "$SRC_DIR" = "" ]; then SRC_DIR="$PWD" fi DIR_PATH="$SRC_DIR" clean_trailing_slash SRC_DIR="$DIR_PATH" PROJECT_NAME_def="/tmp/images_`date +%Y%b%d-%H%M%S`" PROJECT_NAME="" echo "" echo " What \"Project Name\" (output directory name)?" echo "" printf " => Enter full path including New Directory (project) name: [${PROJECT_NAME_def}] " read PROJECT_NAME if [ "$PROJECT_NAME" = "" ]; then PROJECT_NAME="$PROJECT_NAME_def" fi ## Find out if this is for the web or a local browser #TARGET_TYPE="server" # echo # echo " We can produce pages to use on a Web Server," # echo " or else we can make them for you local computer and Web Browser." # echo # printf " => Enter \"l\" or \"local\" for Local system; or \"s\", \"server\" or hit 'ENTER' for Server Type pages: [${TARGET_TYPE}] " # read TARGET_TYPE # if [ "$TARGET_TYPE" = "l" ] || [ "$TARGET_TYPE" = "local" ]; then # TARGET_TYPE="local" # else TARGET_TYPE="server" # fi # #server_or_local () #{ # if [ "$TARGET_TYPE" = "server" ]; then # ALL_PROJECT_DIRS="`basename ${ALL_PROJECT_DIRS}`" # fi #} ###### DIR_PATH="$PROJECT_NAME" clean_trailing_slash PROJECT_NAME="$DIR_PATH" PROJECT="`basename ${PROJECT_NAME}`" THUMBNAILS="${PROJECT_NAME}/thumbnails" MEDIUM="${PROJECT_NAME}/medium" LARGE="${PROJECT_NAME}/large" ORIGINALS="${PROJECT_NAME}/originals" # ImageMagik setup BORDER_WIDTH="-border 10" BORDER_COLOUR="-bordercolor black" ### Start of Main Work ### #STEP ONE: #create directory structures ERR_TARGET="$PROJECT_NAME" if [ -e $PROJECT_NAME ]; then if [ -d $PROJECT_NAME ]; then # make sure we can write to it if ! [ -w $PROJECT_NAME ]; then error_1 fi else error_2 fi else mkdir $PROJECT_NAME #run a test for success if [ -d $PROJECT_NAME ]; then if ! [ -w $PROJECT_NAME ]; then error_3 fi fi fi # does the thumbnails exist, if so is it a dir, if so can we write to it ERR_TARGET="thumbnails" if [ -e $THUMBNAILS ]; then if [ -d $THUMBNAILS ]; then # make sure we can write to it if ! [ -w $THUMBNAILS ]; then error_1 fi else error_2 fi else mkdir $THUMBNAILS #run a test for success if [ -d $THUMBNAILS ]; then if ! [ -w $THUMBNAILS ]; then error_3 fi fi fi ## # now the medium size image dirs ERR_TARGET="medium" if [ -e $MEDIUM ]; then if [ -d $MEDIUM ]; then # make sure we can write to it if ! [ -w $MEDIUM ]; then error_1 fi else error_2 fi else mkdir $MEDIUM #run a test for success if [ -d $MEDIUM ]; then if ! [ -w $MEDIUM ]; then error_3 fi fi fi ## # now the large size image dirs ERR_TARGET="large" if [ -e $LARGE ]; then if [ -d $LARGE ]; then # make sure we can write to it if ! [ -w $LARGE ]; then error_1 fi else error_2 fi else mkdir $LARGE #run a test for success if [ -d $LARGE ]; then if ! [ -w $LARGE ]; then error_3 fi fi fi cd ${SRC_DIR} #run a test for success if [ $PWD != ${SRC_DIR} ]; then error_4 fi #STEP TWO: #create the medium & thumbnails, and move them to their own directory # NOTE: check and see how to test for orientation, maybe the frame option. #mogrify -format gif -size 120x90 -thumbnail 120x90 *.jpg;mv *.gif ${THUMBNAILS}/ #mogrify -format jpg -size 768x512 *.jpg;mv *.jpg ${MEDIUM}/ #mogrify -format jpg -size 768x512 *.jpg;mv *.jpg ${LARGE}/ # does user want to include originals? INCLUDE_ORIGINALS="Y" echo "" echo " Do you want to include the original files for display on the web?" echo "" printf " => Include Originals?: Y/n [${INCLUDE_ORIGINALS}]" read INCLUDE_ORIGINALS if [ "$INCLUDE_ORIGINALS" = "" ]; then INCLUDE_ORIGINALS="y" fi if [ "$INCLUDE_ORIGINALS" != "Y" ] && [ "$INCLUDE_ORIGINALS" != "y" ] ; then echo "" echo "Original files will not be included." echo "" fi ERR_TARGET="originals" if [ -e $ORIGINALS ]; then if [ -d $ORIGINALS ]; then # make sure we can write to it if ! [ -w $ORIGINALS ]; then error_1 fi else error_2 fi else mkdir $ORIGINALS #run a test for success if [ -d $ORIGINALS ]; then if ! [ -w $ORIGINALS ]; then error_3 fi fi fi echo echo # must loop or something to retain file names, then maybe append size for file in `ls -1` do # chech for valid type file $file |grep $IMG_TYPE > /dev/null 2>&1 if [ $? -eq 0 ]; then FULL_NAME=$file BASE_NAME="${FULL_NAME%%.${IMG_EXT}}" BASE_NAME="${BASE_NAME%%.${IMG_EXT_2}}" echo -n "Creating images from ${FULL_NAME}: " echo -n " thumbnail," convert $BORDER_WIDTH $BORDER_COLOUR -format gif -geometry 120x90 -thumbnail 120x90 $FULL_NAME ${THUMBNAILS}/${BASE_NAME}.gif echo -n " medium," # split into two steps so border is consistent (not scaled) convert -geometry 768x512 $FULL_NAME ${MEDIUM}/${BASE_NAME}.jpg mogrify $BORDER_WIDTH $BORDER_COLOUR ${MEDIUM}/${BASE_NAME}.jpg echo -n " large" convert -geometry 1024x768 $FULL_NAME ${LARGE}/${BASE_NAME}.jpg mogrify $BORDER_WIDTH $BORDER_COLOUR ${LARGE}/${BASE_NAME}.jpg echo " ...Done" if [ "$INCLUDE_ORIGINALS" = "Y" ] || [ "$INCLUDE_ORIGINALS" = "y" ] ; then echo -n "Copying original image..." cp ${FULL_NAME} ${ORIGINALS}/${BASE_NAME}.jpg mogrify $BORDER_WIDTH $BORDER_COLOUR ${ORIGINALS}/${BASE_NAME}.jpg echo " Done" fi fi done #STEP 3: echo echo -n "Creating web pages for ${PROJECT}..." # make a main index.html (or shtml?) file for the set of images PAGE_TITLE="${PROJECT}" #"Photo Page" BACKGROUND="bgcolor=\"AntiqueWhite\"" TEXT_and_LINKS="text=\"#668877\" link=\"#336633\" vlink=\"#666633\" alink=\"#0033FF\"" BODY_TAG="" html_code_header () { echo "" > $HTML_PAGE echo "" >> $HTML_PAGE echo " ${PAGE_TITLE}" >> $HTML_PAGE echo "" >> $HTML_PAGE echo "${BODY_TAG}" >> $HTML_PAGE } html_code_footer () { echo "" >> $HTML_PAGE echo "" >> $HTML_PAGE } # The left hand thumnails (will be index.html): HTML_PAGE="${PROJECT_NAME}/index.html" html_code_header #echo "" >> $HTML_PAGE #echo "" >> $HTML_PAGE #echo "" >> $HTML_PAGE #echo "" >> $HTML_PAGE #echo "
" >> $HTML_PAGE echo "" >> $HTML_PAGE echo "" >> $HTML_PAGE for file in `ls -1 ${THUMBNAILS}/` do MED_LRG_file="${file%%.gif}.${IMG_EXT}" echo "" >> $HTML_PAGE echo "" >> $HTML_PAGE echo " " >> $HTML_PAGE echo "" >> $HTML_PAGE done echo "" >> $HTML_PAGE echo "
Up to Main Menu
Large

\ Original
Up to Main Menu
" >> $HTML_PAGE #echo "
" >> $HTML_PAGE html_code_footer # make a copy for index file #cp -f ${PROJECT_NAME}/frame-left.html ${PROJECT_NAME}/index.html ## The Main frame HTML_PAGE="${PROJECT_NAME}/frame-main.html" html_code_header echo "
" >> $HTML_PAGE echo "
" >> $HTML_PAGE echo "
" >> $HTML_PAGE echo "

Picture PlaceMat

" >> $HTML_PAGE echo "
" >> $HTML_PAGE html_code_footer #the top frame HTML_PAGE="${PROJECT_NAME}/frame-top.html" html_code_header echo "
" >> $HTML_PAGE echo "

Picture PlaceMat

" >> $HTML_PAGE echo "

" >> $HTML_PAGE echo "Up to Main Menu" >> $HTML_PAGE html_code_footer # the bottom frame HTML_PAGE="${PROJECT_NAME}/frame-bottom.html" html_code_header echo "" >> $HTML_PAGE echo "" >> $HTML_PAGE #echo "
" >> $HTML_PAGE echo "" >> $HTML_PAGE echo "Picture PlaceMat is a light weight image-to-web publishing unility." >> $HTML_PAGE echo "" >> $HTML_PAGE echo "" >> $HTML_PAGE echo "Created by Pete Nesbitt    www.linux1.ca       November 2005" >> $HTML_PAGE echo "
" >> $HTML_PAGE html_code_footer echo " Done" echo echo " Finished creating Images and Web Pages." echo # let user read output etc... echo "" STEP_DONE="wait" printf " => Press [ENTER] to Continue" read STEP_DONE unset STEP_DONE print_SCREEN ########## ../index.html # does user want an index page built? #MASTER_INDEX="${PROJECT_NAME}/.." CURRENT_PATH="`pwd`" cd ${PROJECT_NAME} cd ../ MASTER_PATH="`pwd`" cd $CURRENT_PATH UPPER_INDEX="Y" echo "" echo " Do you want to create an Index Page ONE LEVEL UP from this project?" echo " It will include this Project and any others in parallel directories." echo "" # see if it exists EXISTS="n" if [ -e ${MASTER_PATH}/index.html ] || [ -e ${MASTER_PATH}/index.htm ]; then if [ -d ${MASTER_PATH}/index.html ] || [ -d ${MASTER_PATH}/index.htm ]; then EXISTS="d" echo "Strange, the index file \(index.htm or index.html\) looks like a directory!" echo "An upper level Index Page will not be created or edited." else EXISTS="y" printf " => Over-ride ../index.html?: Y/n [${UPPER_INDEX}]" read UPPER_INDEX if [ "$UPPER_INDEX" = "" ] || [ "$UPPER_INDEX" = "y" ]; then UPPER_INDEX="Y" fi if [ "$UPPER_INDEX" != "Y" ]; then echo "" echo "An upper level Index Page will not be created or edited." echo "" fi fi fi ERR_TARGET="upper level index page" if ! [ -w ${MASTER_PATH} ]; then error_1 fi ############ TOP LEVEL PAGES ############# ########################################## # First, creat for Local Use (no -server) ########################################## # frames definitions for main area HTML_PAGE="${MASTER_PATH}/index.html" echo "" > $HTML_PAGE echo "" >> $HTML_PAGE echo " ${PAGE_TITLE}" >> $HTML_PAGE echo " " >> $HTML_PAGE echo " " >> $HTML_PAGE echo " " >> $HTML_PAGE echo " " >> $HTML_PAGE echo " " >> $HTML_PAGE echo " " >> $HTML_PAGE echo " $BODY_TAG" >> $HTML_PAGE echo " <center>" >> $HTML_PAGE echo " <h1>Photo Page</h1>" >> $HTML_PAGE echo " <br>" >> $HTML_PAGE echo " <h3>You need <b>frames</b> for this to work.</h3>>" >> $HTML_PAGE echo " </center>" >> $HTML_PAGE echo " </body>" >> $HTML_PAGE echo " " >> $HTML_PAGE echo " " >> $HTML_PAGE echo " " >> $HTML_PAGE echo " " >> $HTML_PAGE echo "" >> $HTML_PAGE ## # this could be an empty left frame if you comment the 'echo' line HTML_PAGE="${MASTER_PATH}/frame-left.html" html_code_header echo "Picture PlaceMat is a very basic photo publisher. I had searched for a resonable publishing software that did not require anything extra on the server (like running cgis or requiring ssi parsing) and wanted the maintenance of a picture site to be simple. I couldn't find anything to fit the bill so I wrote Picture PlaceMat. It is a slightly long BASH shell script, with a few manual upload steps to follow." >> $HTML_PAGE html_code_footer # home page at top level, displays a thumb/link of each area. HTML_PAGE="${MASTER_PATH}/home.html" if [ ${UPPER_INDEX} = "Y" ] ; then if ! [ ${EXISTS} = "d" ]; then # make the page # test for success echo " " > $HTML_PAGE if [ $? -eq 0 ]; then INDEX_OK="y" else INDEX_OK="n" fi html_code_header echo "" >> $HTML_PAGE echo " " >> $HTML_PAGE TD_COUNT="0" # set a var for sumary page #for each dir in the ls of ../ then look for a thumbnails dir and grab an image for ALL_DIRS in `ls ${MASTER_PATH}` do ALL_PROJECT_DIRS="${MASTER_PATH}/${ALL_DIRS}" if [ -d $ALL_PROJECT_DIRS ]; then if [ -e ${ALL_PROJECT_DIRS}/thumbnails ] && [ -d ${ALL_PROJECT_DIRS}/thumbnails ]; then #for each dir print thumb and dirname # add to upper level index page # HTML_PAGE="${MASTER_PATH}/index.html" #echo "
" >> $HTML_PAGE # grab the first image # TD_COUNT="0" THUMB_COUNT="0" # check if .gif for file in `ls -1 ${ALL_PROJECT_DIRS}/thumbnails/` do if [ $THUMB_COUNT -eq 0 ];then while [ $THUMB_COUNT -lt 1 ] do TD_COUNT=`echo "$TD_COUNT + 1"| bc` if [ $TD_COUNT -ge 5 ]; then TD_COUNT="1" echo "" >> $HTML_PAGE fi RELATIVE_LINK="`basename ${ALL_PROJECT_DIRS}`" echo "" >> $HTML_PAGE THUMB_COUNT=`echo "$THUMB_COUNT + 1"| bc` done #else #THUMB_COUNT=`echo "$THUMB_COUNT + 1"| bc` #THUMB_COUNT="0" fi THUMB_COUNT=`echo "$THUMB_COUNT + 1"| bc` done # echo "

${ALL_DIRS}
" >> $HTML_PAGE fi fi done echo "" >> $HTML_PAGE html_code_footer fi fi # print out a status page image_names () { for file in `ls -1 ${THUMBNAILS}/` do FILE_SHORTNAME="${file%%.gif}" echo " $FILE_SHORTNAME" done } # # end of top level home.html #the top frame HTML_PAGE="${MASTER_PATH}/frame-top.html" html_code_header echo "
" >> $HTML_PAGE echo "

Picture PlaceMat

" >> $HTML_PAGE echo "

" >> $HTML_PAGE html_code_footer # the bottom frame HTML_PAGE="${MASTER_PATH}/frame-bottom.html" html_code_header echo "" >> $HTML_PAGE echo "" >> $HTML_PAGE #echo "
" >> $HTML_PAGE echo "" >> $HTML_PAGE echo "Picture PlaceMat is a light weight image-to-web publishing unility." >> $HTML_PAGE echo "" >> $HTML_PAGE echo "" >> $HTML_PAGE echo "Created by Pete Nesbitt    www.linux1.ca       November 2005" >> $HTML_PAGE echo "
" >> $HTML_PAGE html_code_footer # Final Summary ################################################### print_SCREEN echo echo echo " We have successfully created the following:" echo echo " Project: ${PROJECT}" echo echo " Images:" image_names echo echo " Located in: $PROJECT_NAME" echo # report on $INDEX_OK if [ "${INDEX_OK}" = "y" ]; then echo " We have successfully created" echo " ${MASTER_PATH}/index.html" else echo " The Index Page failed!!!" fi ########## # echo echo "" echo echo "To see the new Project, point your browser to \"file://${MASTER_PATH}/index.html\"" echo exit #other things that could be added #4)make an shtml index that uses a var for each image thumbnail clicked on, could include back/next buttons (but would need shtml support on server) #5) scp or sftp the file set to a server as long as write permission is granted.