Bash Script to Create an Online Photo Album

Click Here! to Download 10,000 Fonts
220px-Poundexclam.svgThis bash script can be used to create the webpages for viewing your online pictures. Simply, place the pictures directory one level below your script, and pass the picture directory as an argument to the script eg: scriptname.sh directory. Once the script finish creating all the pages, it will output a web link, so you can send to your friends. Following is the code, and you can download the script from here
This is how the photo album would look like: PhotoAlbum

#!/bin/bash
#photo album script
#script to generate webpages for wiewing pictures
#script takes one argument which is the picture directory, which should be one level 
#down this script, and changes directory and creates all the web pages. Finally, 
#builds a link to be send to friends for viewing of the album
#by 
#Jorge L. Vazquez
#03/21/2014
 
#START
 
#printing usage error to screen
if [ $# -ne 1 ]; then
	echo "USAGE: $(basename $0) directory"
	exit 1
fi
 
#variables
EXT="JPG"				#your pics file extension
TITLE="$1"				#album title
DIRECTORY="$1"				#pictures directory
ALLPICS=""				#array to hold pictures
TOTAL=""				#total number of pics
THISPIC=""				#store current pic
THISPAGE=""				#store current picture
PREVPAGE=""				#store link to previous page
NEXTPAGE=""				#store link to next page
LINKPAGE="http://yourwebsite.com"	#link of first page to be emailed
 
#html code for webpage
webpage() {
 
	cat << EOF
	<html>
	<body>
 
	<div id="container" style="text-align:center">
	<h2>$TITLE</h2>
	<h4>Picture $NUM of $TOTAL</h4>
	<img src="$THISPIC" style="align:center" height="500" width="800">
	<br>
	<a href="$PREVPAGE">Previous</a>
	&nbsp;
	&nbsp;
	&nbsp;
	&nbsp;
	<a href="$NEXTPAGE">Next</a>
 
	</div>
 
	</body>
	</html>
EOF
}
 
 
##########################################
#       MAIN
##########################################
 
if [ -d $DIRECTORY ]; then
	cd $PWD/$DIRECTORY
else
	echo "Dirctory $DIRECTORY does not exists!.."
fi
 
#creating array of pictures
ALLPICS=(*.$EXT)
TOTAL=${#ALLPICS[@]}
 
#looping through all pictures and create a html page for each
for((i=0; i<$TOTAL; i++)); do
	THISPIC="${ALLPICS[i]}"
	THISPAGE="${ALLPICS[i]%.$EXT}.html"
	#the first picture 
	if [ $i -eq 0 ]; then
		PREVPAGE=""
		NEXTPAGE="${ALLPICS[$((i+1))]%.$EXT}.html"
		#creating web link for sending to friends
		LINKPAGE="$LINKPAGE/$DIRECTORY/$THISPAGE"
 
	#the last picture
	elif [ $i -eq $(($TOTAL-1)) ]; then
		NEXTPAGE=""
		PREVPAGE="${ALLPICS[$((i-1))]%.$EXT}.html"
 
	else
		#all other pages except 1st and last
		PREVPAGE="${ALLPICS[$((i-1))]%.$EXT}.html"
		NEXTPAGE="${ALLPICS[$((i+1))]%.$EXT}.html"
	fi	
	#current picture
	NUM=$((i+1))		
 
	#creating the page
	webpage $THISPIC $TOTAL $NUM $PREVPAGE $NEXTPAGE $TITLE > $THISPAGE
done
 
echo "Album Created..."
echo "Email link to friends: $LINKPAGE"
 
#END
Share This!

One thought on “Bash Script to Create an Online Photo Album

  1. Pingback: Bash Script to Create Photo Album: Improved | PCtechtips

Leave a Reply

Your email address will not be published.