#!/usr/bin/perl
# Philip Shuman philip at shuman dot org
# album.pl v0.2.1
# web photo album index.html generator and photo resizer
#
# requires ImageMagick's `mogrify` utility,
# Image::Size from http://www.blackperl.com/Image::Size/
# and Getopt::Std.
#
# Usage:
#   cd into the directory with your .jpg files.
#   run album.pl
#   give it the -g 640x480,800x600,1024x768 to create different
#   scaled images. (without -g will default to 640x480,1024x768)
#
# Change this to your email, name, etc.
$comment = "youremail\@shuman.org";

use Image::Size;
use Getopt::Std;
if (getopts("g:")) {
}
else {
   exit -1;
}
if (!$opt_g) {
   @geos = ('640x480', '1024x768');
   print "no -g, defaulting to 640x480,1024x768\n";
}
else {
   @geos = split(',', $opt_g);
}


@files = `/bin/ls -1 *.jpg`;
`echo "<!-- Created with Phil's album.pl http://www.shuman.org-->" > index.html`;
`echo "<table><tr>" >> index.html`;
$index = 0;

foreach $file ( @files ) {
   chomp($file);
   ($x, $y) = imgsize("$file");
   $orgsize = "$x" . "x$y";
   print "$file $x" . "x$y\n";

   $tmp = $file;
   $tmp =~ s/\.jpg/_thm.jpg/;
   print "   Creating $tmp\n";
   `cp $file $tmp`;
   `/usr/local/bin/mogrify -geometry 128x96 -quality 60 $tmp`;
   `echo "   <td valign=top><a href=$file>" >> index.html`;
   `echo "      <img src=$tmp border=0></a><br>" >> index.html`;

   foreach $res (@geos) {
      $tmp = $file;
      $tmp =~ s/\.jpg/_$res.jpg/;
      print "   Creating $tmp\n";
      `cp $file $tmp`;
      `/usr/local/bin/mogrify -comment "$comment" -geometry $res -quality 70 $tmp `;
      `echo "      <a href=$tmp>$res</a><br>" >> index.html`;
   }
   `echo "      <a href=$file>$orgsize</a>" >> index.html`;

   `echo "   </td>" >> index.html`;

   $index++;
   if ($index >= 6) {
      `echo "</tr><tr>" >> index.html`;
      $index=0;
   }
   
}
`echo "</tr></table>" >> index.html`;
