#!/usr/bin/perl

$title = '';
$thumb = "./thumb";                             # directory where thumbnails are stored
$catalog = "catalog.html";                      # catalog html page
$thumbquality =  70;                            # Thumbnail image quality, range 0 - 100
$thumbwidth = 260;                              # Thumbnail width
$thumbheight = 196;                              # Thumbnail height

$im_identify  = "/usr/bin/identify";      # Path to Imagemagick "identify" program
$im_convert   = "/usr/bin/convert";       # Path to Imagemagick "convert" 

# get list of files

    @files = `ls ./ -1`; # get list of images
    $n=0;
    foreach $file (@files) {
        chomp($file);
        if ($file =~ /jpg|JPG|gif|GIF/) {push (@images,$file); }
        }


# output catalog html


open(CATALOG,">$catalog") || die "Can't open: $catalog\n";

print CATALOG "<html>\n";
print CATALOG "<head>\n";
print CATALOG "<title>$title</title>\n";
print CATALOG "</head>\n";
print CATALOG "<body>\n";
print CATALOG "</body>\n";
print CATALOG "</html>\n";
print CATALOG "<h1>$title</h1></td>\n";

# verify thumbnail directory exists

if (!(-d $thumb)) {mkdir $thumb, 0777; }

# create missing thumb nails

foreach $image (@images) {

    if (!(-f "$thumb/$image")) {
        print "making $thumb/$image<br>\n"; 
        $arguments = "-geometry $thumbwidth" ."x"."$thumbheight -quality $thumbquality $image $thumb/$image";
        $results=`$im_convert $arguments`;
        }
    }

foreach $image (@images) {
    chomp ($image);
    ($title,$extension) = split(/\./, $image);
    $title=~ s/\_|-/ /g;
#    $results=`$im_identify $image`;
#    ($junk,$format,$dimensions,$junk,$depth,$size,$junk)=split(/\ /,$results);
#    ($dimensions,$junk)=split(/\+/,$dimensions);
#    $title = $title . ' (' . $dimensions . ' ' . $size . ')';
    print CATALOG "<a href=\"$image\"><img src=\"$thumb/$image\" border=\"1\" alt=\"$title\"></a>\n";
    }

print CATALOG "</body>\n";
print CATALOG "</html>\n";

close (CATALOG);

exit;
