#!/usr/bin/perl
# GenIndex 12/27/03
# (c) 2003 by Stephen A. Ness
# Generate thumbnails and HTML index page for given directory (default: current directory).
# Usage: GenIndex [ -d ] [ -f ] [ -i ] [-ln ] [ -r ] [ -v ] [ dir ]
# -d print debug info
# -f force thumbnail regeneration even if it already exists
# -i create image index only (don't index directories or ordinary files)
# -ln generate n levels of parent links (if not in /home/photo)
# -r generate subdirectory indices recursively
# -v print verbose info
# Uses EzThumbs and jpginfo.
$version = "1.4"; # version number
# Configuration.
# Cf. also gen_links, which has wired-in knowledge about /home/photo.
# Cf. also wired-in info in $html_hd and $html_ft.
$index = "index.html"; # default index filename
# Directories where index.html is maintained by hand; avoid clobbering these.
$avoid = "\/home\/photo|\/home\/photo\/san\/\\d\\d\\d\\d";
$exclude = "$index|g$indexl|thumbs|Thumbs.db|_xlk|xxx"; # file/dir names to exclude from index
$html_exts = "gif|htm|html|txt"; # extensions which support hrefs
$image_exts = "jpeg|jpg|tif"; # FIX_ME EzThumbs does not deal with GIFs, so gif in html_exts instead
# Parameters for thumbnail size and index page image layout.
# For uniformity, make all thumbnails 100 pixels high.
# Most pictures are 3x4 (portrait) or 4x3 (landscape) aspect ratio,
# so most thumbnails are 75x100 or 133x100.
# The generated HTML index page contains a table of pix;
# each table entry consists of a thumbnail image with filename below.
# Thumbnails may span multiple columns or (in the case of panoramas)
# may even be wider than the table.
$col_w = 150; # HTML table column width
$row_h = 125; # HTML table row height
$row_w = 900; # HTML table row width
$ncols = int($row_w / $col_w); # number of columns per table row
# Executable and parameters for EzThumbs invocation.
$EzThumbs = "/u/EasyThumbnails/EZThumbs";
$tn_dir = "thumbs";
$tn_h = 100;
$tn_pfx = "tn_";
$tn_q = "75";
$tn_w = 10000; # so EzThumbs will always generate thumbnail height 100 pixels
# Executable and switches for jpginfo invocation.
$jpginfo = "perl /home/photo/bin/jpginfo -c -t";
# Executable for recursive GenIndex invocation.
$GenIndex = "perl /home/photo/bin/GenIndex";
# Subroutines.
# Examine contents of current directory and build file and directory lists.
# Exclude all ".*" files, [g]index.html, Thumbs.db, subdirectory thumbs or _xlk.
# This respects case sensitivity, so it misses e.g. "FOO.JPG" as image file.
# @dirs directories
# @ifiles image files (.jpeg, .jpg, .tif)
# @ofiles other files
sub check_dir {
$ofile = grep(/^($avoid)$/, $dir) ? "g$index" : $index; # output filename (index.html or gindex.html)
print "[$dir:]\n" if $vflag;
opendir(DIR, ".") || die "cannot open \".\"\n";
@files = grep(!/^($exclude)$/, grep(!/^\./, readdir(DIR)));
closedir(DIR);
@dirs = grep(-d, @files);
@files = grep(-f, @files);
@ifiles = grep(/\.($image_exts)$/, @files);
@ofiles = grep(!/\.($image_exts)$/, @files);
}
# Generate an image thumbnail (if necessary) and write an href in the index.
sub do_image {
local ($f) = @_;
mkdir($tn_dir, 0755) unless -d $tn_dir; # create thumbnail directory if necessary
print "$f:\n" if $vflag;
$tn = "$tn_dir/$tn_pfx$f"; # thumbnail filename
$tn =~ s/\.(jpeg|tif)$/.jpg/; # EzThumbs generates tn_foo.jpg for foo.jpeg and foo.tif
# Generate the thumbnail via EzThumbs if it does not exist or if fflag.
`$EzThumbs $f /D=$tn_dir /P=$tn_pfx /H=$tn_h /W=$tn_w /Q=$tn_q` if $fflag || ! -f $tn;
chop($w = `$jpginfo $tn`); # get thumbnail width info via jpginfo
$w =~ s/.*w=(\d+) .*/$1/; # grab thumbnail width spec
die "$tn: unexpected thumbnail width $w\n" if $w == 0;
print "width=$w row_w=$row_w row_rem=$row_rem in_row=$in_row in_table=$in_table\n" if $dflag;
if ($w > $row_w) {
# Very wide thumbnail, e.g. panorama; terminate table.
&table_end if $in_table;
print OUTF "
$f
\n";
return;
}
&row_end if $in_row && $w > $row_rem; # too wide for current row, flush
&row_start unless $in_row; # start new row if necessary
$span = int(($w + $col_w - 1) / $col_w); # column span of this thumbnail
$row_rem -= $span * $col_w; # remaining space in this row
$align = $span == 1 ? "center" : "left"; # center if single column, else left align
print OUTF "
\n" x $ncols; print OUTF " |