#!/usr/local/bin/perl use strict; use Template; use Image::Size; use File::Find; # Location of template files. my $template_dir = "/web/philip/www/images/jsc-script-test"; my $index_template = "$template_dir/index.tt"; my $target_template = "$template_dir/thumbnail.tt"; my $webroot = "/web/philip/www" my $copyright = shift; if (!$copyright) { my $year = (localtime(time))[5] + 1900; $copyright = "copyright $year Philip Greenspun"; } my %fileinfo; # Get all the file information we're going to need find(\&wanted, "."); my $tt = Template->new({ABSOLUTE => 1}); # Generate index page my @photos = values %fileinfo; $tt->process($index_template, { photos => \@photos }, "index.html") || die $tt->error; # Generate thumbnail targets. foreach my $photo (@photos) { $tt->process($target_template, { photo => $photo, copyright => $copyright }, $photo->{'target_url'} ) || die $tt->error; } sub wanted { my %info; my $path = $File::Find::name; # Rename .JPG and .JPEG to .jpg if ($path =~ /^(.*)\.JPE?G$/) { rename($path, "$1.jpg") || die $!; $path = "$1.jpg"; } # Get rid of relative "./" beginning. $path =~ s,^\./,,; # If we have one of the resized JPEG files... if ($path =~ /^(.*)\.(\d+)\.jpg$/) { # Grab everything up to the .1.jpg part. my $basename = $1; my $size = $2; # Uniquify it on the base path. my $info; if ($fileinfo{$basename}) { $info = $fileinfo{$basename}; } else { $info = $fileinfo{$basename} = {}; } # Fill in the interesting fields. $info->{'basename'} = $basename; # Mark that we have this size. $info->{'sizes'}[$size]++; # Find and save the image size. my ($width, $height) = imgsize($path); ($info->{'widths'}[$size], $info->{'heights'}[$size]) = ($width, $height); # For convenience... $info->{'html_imgsizes'}[$size] = "width=$width height=$height"; $info->{'img_urls'}[$size] = $path; $info->{'target_url'} = "$basename.tcl"; } }