GraphThumbnailer

From FrogspawnWiki

Jump to: navigation, search

Contents

Background

I keep an excel spreadsheet that tracks my weight on my weekly weigh-ins. This spreadsheet contains a graph that shows the change. I like to publicise the spreadsheet so export it to html and upload it to my website. Within the main website there is a sidebar image that links to the weight page using a thumbnail of the graph.

The problem

Excel doesn't always use the same filename for the graph. By default on windows it saves as a GIF, on MAC it saves as a PNG. The actual filename also varies depending on if the whole page was exported or just a selection, and is not even consistent between two exports of the same selection.

The solution

This BASH script runs from crontab. It compares the timestamp on the html page to that on the thumbnail to see if there's been an update. If it detects an update it uses awk to extract from the html the filename that has been used for the graph, it then further checks the timestamps on the main graph image and the thumbnail, then uses the convert tool from ImageMagick to generate the thumbnails in png format.

code

cd ~/public_html/weight/weight_files
 
# first look to see if the htm file has been updated
# by comparing it to the exisiting thumbnail
if [ ../weight.htm -nt 200thumb.png ]; then
 
	echo Update detected at $(date)
 
	# at this point we know there's been an update to the htm
	# we need to find out what the new big image is called. 
	# We store that as $imagefile
	longstring=$(grep image[0-9][0-9][0-9] ../weight.htm)
	imagefile=$(echo $longstring | awk -F 't_files/|" v:shapes=' '{print $2}')
 
	echo Current graph image is $imagefile
 
	# check that the big image is newer than the thumbnail
	if [ $imagefile -nt 200thumb.png ]; then
		echo Updating thumbnails
		# create thumbnails
		convert -resize 200 $imagefile 200thumb.png
		convert -resize 300 $imagefile thumb.png
	else
		# no change needed, but update timestamp or script will trigger forever
		echo Image not changed
		touch 200thumb.png
	fi
 
	echo Done.
 
fi

output

As it executes, if a change is detected something similar to the following is output (and emailed from cron)

Update detected at Fri Apr 24 08:20:01 BST 2009
Current graph image is weight_28973_image001.png
Updating thumbnails
Done.
Personal tools