#!/usr/bin/perl -w
# -*- Mode: Perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-

=for the record

Should make fine html pages out of a dir of photos

This version for just 2col s=400x300 -> m=800x600 l=1600x1200

run with
 ls | pag-s2ml > index.html

expects small images (and possible a "writeme" file) in cur dir and
med in subdir "m" and large in subdir "l"

"writeme" is where to put text you'd like to see on the page.  pag-*
takes the text (which should be marked up ready for insertion into an
html file) and writes it out between the /table and /body tags.

Next:

should take input for page title.  getopts :^/ ?

 ls | pag-s2ml -t "gunrack photos" > index.html

=cut to code


use Image::Size;
use File::Basename;
#use subs ("eachpic");

{
    print
"<HTML><HEAD>
<meta name=\"GENERATOR\" content=\"E'beth pag-s2ml 0.1\">
<TITLE>photo page</TITLE>
</HEAD>
<BODY>

<P>\(Images link to larger versions.  Read about <A HREF=\"http://www.fools-errant.com/~ebeth/coding/photopage/about.html\">photopage/pag-s2ml</A>?\) </P>

<TABLE border=0 cellspacing=5><TR>\n";
    
    FILE: for($i=0; ; $i++)
    {
        do
        {
            unless (defined ($_ = <STDIN>))
                # intended to be a line from ls = one filename\n
            { 
                print "<TD></TD>\n" if $i%2; # odd table bin
                last FILE;
            }
            $html = eachpic($_);
        } until $html;

        print $html;
        print "</TR><TR>\n" if $i%2; # two TD's per TR.
    }    
    print "</TR></TABLE>\n\n";

    if (open WRITEME, '<writeme') # optional text to add to page after pix
    {
        print while <WRITEME>;
        close WRITEME;
    }

    print "</BODY></HTML>\n\n";
}


sub eachpic
{
    shift; # @_ = filename\n
    chomp;
    return 0 if !-f;
    
    my ($base, $path, $suff)
        = fileparse ($_, qw(\.gif \.GIF \.jpg \.JPG \.p.?m \.P.?M));
    return 0 if $suff eq ""; # not a browserable image
    
    my $filename = $base . $suff;
    
    my ($width, $height) = imgsize($_);
    my $w2 = int($width/2);
    my $w21 = $w2 -1;
    my $h = 3*int($height/4) -1;
    my $w1 = $width -1;

    $retval =
    # BTW, why doesn't "my retval=" work?
"<TD ALIGN=CENTER>
<IMG SRC=\"$filename\" usemap=\"#$filename\" BORDER=0 WIDTH=\"$width\" HEIGHT=\"$height\">
<MAP name=\"$filename\">
 <AREA href=\"m/$filename\" shape=\"rect\" coords=\"0,0,$w21,$h\">
 <AREA href=\"l/$filename\" shape=\"rect\" coords=\"$w2,0,$w1,$h\">
</MAP>
</TD>

";
    return $retval;
}

