#!/bin/bash
# bashgal - http://shapor.com/bashgal
# Author: Shapor Naghibzadeh <shapor@gmail.com>
# configuration section
convert="nice gm convert" # GraphicsMagick (http://www.graphicsmagick.org) > ImageMagick
jhead="nice jhead"
fadvise="nice fadvise -dontneed "
thumbx=133 thumby=100
sheetx=1200 sheety=10000
heights=(480 600 800 1024)
thumbdir=.th sheetdir=.sh
index=index.html
defaultres=600
tmpsheet=/tmp/sheet-$$.jpg tmprow=/tmp/row-$$.jpg tmpindex=/tmp/index-$$.html
# end configuration section
function debug {
return 0 # 0=enable, 1=disable debugging output
}
[[ $# -gt 1 ]] && { echo "$0: usage $0 [directory]"; exit 1; }
[[ $# -eq 0 ]] || pushd $1 >/dev/null || exit 1
[[ -d $thumbdir ]] || mkdir $thumbdir || exit 2
for res in ${heights[*]}; do
[[ -d .$res/.html ]] || mkdir -p .$res/.html || exit 3
echo "<html><body><style>div{height:100px;margin:1;padding:0;position:relative;overflow:hidden;float:left}</style>" >$tmpindex.$res
for size in ${heights[*]}; do
[[ $size == $res ]] && echo -n "$size " || echo -n "<a href='.$size.$index'>$size</a> "
done >>$tmpindex.$res
done
numfiles=0
for filename in *.[jJ][pP][gG]; do
debug && echo -n "$filename: "
$jhead -autorot "$filename" 2>/dev/null | grep ^Modified: >/dev/null && echo -n "rotated "
[[ -s $thumbdir/$filename ]] || $convert -scale x$thumby "$filename" "$thumbdir/$filename" || continue
filelist[$numfiles]=$filename
let numfiles++
for res in ${heights[*]}; do
debug && echo -n "$res "
if [[ ! -s .$res/$filename ]]; then
$convert -scale x$res "$filename" ".$res/$filename"
$fadvise ".$res/$filename" 2>/dev/null
fi
done
$fadvise "$filename" 2>/dev/null
debug && echo
done
# The rest is basically an optimization to take advantage of spriting css
# generate thumbnail sheets up to sheetx x sheety size
[[ -d $sheetdir ]] || mkdir $sheetdir || exit 10
sheet=0 cury=0 curx=0 file=0
while [[ $file -lt $numfiles ]]; do
filename=${filelist[$file]}
thumb=$thumbdir/$filename
twidth=$($jhead "$thumb" 2>/dev/null | awk '($1 == "Resolution") {print $3}')
# generate the thumbnail sheets
if [[ $((curx + twidth)) -gt $sheetx ]]; then
debug && echo
$convert -append $tmpsheet $tmprow $tmpsheet 2>/dev/null
rm $tmprow
curx=0
cury=$((cury + thumby))
if [[ $((cury + thumby)) -gt $sheety ]]; then
debug && echo saving sheet $sheet
mv $tmpsheet $sheetdir/$sheet.jpg
cury=0
let sheet++
fi
fi
debug && echo -n "$filename "
$convert +append $tmprow "$thumb" $tmprow 2>/dev/null
# generate the html in .1024/.html/file.jpg.html
for res in ${heights[*]}; do
prev= next=
[[ $file -ne 0 ]] && prev=${filelist[$((file - 1))]}
[[ $file -ne $((numfiles - 1)) ]] && next=${filelist[$((file + 1))]}
htmlfile=.$res/.html/$filename.html
xsize=$($jhead ".$res/$filename" 2>/dev/null | awk '($1 == "Resolution") {print $3}')
cat >"$htmlfile" <<EOF
<html><head><title>$filename</title>
<script language="JavaScript">
prev=new Image();
next=new Image();
function f(){
EOF
[[ $prev ]] && echo "prev.src='../$prev';" >>"$htmlfile"
[[ $next ]] && echo "next.src='../$next';" >>"$htmlfile"
cat >>"$htmlfile" <<EOF
}
</script></head><body onLoad="f()">
<a href='../../.$res.$index'>^ up to thumbnails ^</a>
EOF
[[ $prev ]] && echo "<a href='$prev.html'><- prev</a> " >>"$htmlfile" || echo -n "<- prev " >>"$htmlfile"
[[ $next ]] && echo "<a href='$next.html'>next -></a> " >>"$htmlfile" || echo -n "next -> " >>"$htmlfile"
for size in ${heights[*]}; do
[[ $size == $res ]] && echo -n "$size " || echo -n "<a href='../../.$size/.html/$filename.html'>$size</a> "
done >>"$htmlfile"
echo "<p><a href="../../$filename"><img border=0 width=$xsize height=$res src='../$filename'></a><p>" >>"$htmlfile"
$jhead "$filename" 2>/dev/null | sed 's/$/<br>/' >>"$htmlfile"
echo '<hr><font size="-1">Generated by <a href="http://shapor.com/bashgal">bashgal</a></font></body>' >>"$htmlfile"
echo "<div style='width:${twidth}px'><a href='.$res/.html/$filename.html'><img border=0 alt='$filename' src='$sheetdir/$sheet.jpg' style='left:-${curx}px;top:-${cury}px;position:absolute'></a></div>" >>$tmpindex.$res
done
let file++
curx=$((curx + twidth))
done
$convert -append $tmpsheet $tmprow $tmpsheet 2>/dev/null
rm $tmprow 2>/dev/null
debug && echo && echo saving sheet $sheet
mv $tmpsheet $sheetdir/$sheet.jpg 2>/dev/null
# generate html
for res in ${heights[*]}; do
echo "</body></html>" >>$tmpindex.$res
mv $tmpindex.$res .$res.$index
done
rm $index 2>/dev/null
ln -s .$defaultres.$index $index
[[ $# -eq 1 ]] && popd >/dev/null