#!/usr/bin/perl
# make sure, that the path of perl is right (try: which perl5)

###############################################################
#
# Pix2tn by Michael Hahsler 1999
# 
# This script free for use and modification.
# Version: 1.0 (18-Oct-1999)
#
# Usage: Copy this script into the directory with your photos or in
#   your directory for executables (e.g. ~/bin). Change to the directory
#   with the photos and run the script. The script will generate the
#   file welcome.html (the index) and the sub-directories tn and med 
#   (for the thumpnails).
#   If you want to have the index and the pictures on the Web, the directory
#   you have to move everything to a directory used by your Web-server 
#   (e.g. ~\www).
#
# Copyright: This script is under GPL
#
# You can reach me under hahsler@ai.wu-wien.ac.at
#

my $title="Title goes here";
my $tnperrow=4;   # thumbnails per row
my $tnsize=80;    # size of thumbnails
my $tnquality=25; # quality of thumbnails [0..100]
# use small thumbnails with poor quality to speed up your index-page

my $medsize=500;    # size of thumbnails
my $medquality=70; # quality of thumbnails [0..100]
# used to create medium sized pictures

# possible image extensions (jpg, gif)
my @pics = (<*.jpg>,<*.JPG>,<*.gif>,<*.GIF>);

#die "Problem: welcome.html exists!!!" if (-e "index.html");
open (PAGE, ">index.html") || die "Problem: Can't write to index.html\n";

# create a directory for the thumbnails
system ("mkdir tn") if (!-d "tn");
system ("mkdir med") if (!-d "med");

# create the index page
print PAGE qq*
<html><head><title>$title</title></head>
<body bgcolor=white><h1>$title</h1>
<table cellspacing=10>
*;

my $counter=0;

foreach $_ (sort @pics) {
  print $_;
  system ("convert -geometry ".$tnsize."x".$tnsize.
      " -quality $tnquality $_ tn/$_") == 0 
      || die "Problems with convert: $?\n";


  system ("convert -geometry ".$medsize."x".$medsize.
      " -quality $medquality $_ med/$_") == 0
      || die "Problems with convert: $?\n";



print PAGE "<tr valign=bottom>" if (!($counter++%$tnperrow));
  print PAGE "<td>";

  @stat = stat $_;
  print PAGE qq*<center>
    <a href="med/$_"><img src="tn/$_"></a><br>
    <small><b>$_</b><br>*.
    localtime($stat[9]).qq*<br><a href="$_">original - *.
    int($stat[7]/1024).qq* kB</a>*.
    qq*</small>\n*;
  print " ... done\n";
}

print PAGE qq*
  </table>
  Index created: *.localtime(time).qq*.
  </body></html>
*;

close PAGE;
