#!/usr/local/bin/perl # authored by a friend of philg@mit.edu in 2005 use strict; use Template; use Image::Size; use File::Find; # Location of template files. my $template_dir = "/web/philip/www/images/tools"; my $index_template = "$template_dir/index.tt"; my $target_template = "$template_dir/thumbnail.tt"; my $copyright = shift; if (!$copyright) { my $year = (localtime(time))[5] + 1900; $copyright = "copyright $year Philip Greenspun"; } # Get all the file information we're going to need my %fileinfo; find(\&wanted, "."); my @photos = sort { $a->{'basename'} cmp $b->{'basename'} } values %fileinfo; chomp(my $cwd = `pwd`); my ($url_basedir) = ($cwd =~ m,/web/[^/]+/www(/.*)$,); my ($directory_pretty_name) = ($url_basedir =~ m,/images(/.*)$,); my $tt = Template->new({ABSOLUTE => 1}); # Generate index page my $index_all_link = "Click here for all images in this directory."; $tt->process($index_template, { url_basedir => $url_basedir, photos => \@photos, directory_pretty_name => $directory_pretty_name, index_all_link => $index_all_link }, "index.html") || die $tt->error; $tt->process($index_template, { url_basedir => $url_basedir, photos => \@photos, directory_pretty_name => $directory_pretty_name }, "index-all.html") || die $tt->error; # Generate thumbnail targets. foreach my $photo (@photos) { $tt->process($target_template, { photo => $photo, copyright => $copyright, url_basedir => $url_basedir }, $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"; } }