#!/usr/bin/perl # # A stupid little script to make thumbnails and a webpage # from a directory of image files. # # Requirements: # # * Image::Size perl module # # as root, type: # perl -MCPAN -e 'install Image::Size' # # * ImageMagick (search Google.com ... you'll find it) # # Bug reports, comments, and patches are not welcome. If # I want a new feature or a bug fixed, I'll do it. :) # I'm providing this just so you can have it if you want it. # # This code is released into the public domain. Do with # it as you please. # # Brad Fitzpatrick # bradfitz@bradfitz.com # 2000-12-27 # use constant THUMB_COLUMNS => 5; use strict; use Image::Size; my $dir = shift @ARGV; $dir ||= "."; unless (-d $dir) { die "Directory given is not a directory!\n"; } if (-e "$dir/index.html") { open (INDEX, "$dir/index.html"); my $line = ; if ($line ne "\r\n") { die "index.html in this directory is not a thumbnails image\n"; } close INDEX; } my @files; my @thumbs; opendir (DIR, $dir); @files = sort readdir(DIR); foreach my $file (@files) { next if ($file =~ /^\.\.?$/); next if ($file =~ /-thumb\./); next if ($file =~ /-med\./); next unless ($file =~ /\.(JPE?G)|(GIF)|(PNG)$/i); my $thumbfile = $file; my $medfile = $file; $thumbfile =~ s/(\..+?)/-thumb$1/; $medfile =~ s/(\..+?)/-med$1/; my ($ox, $oy) = imgsize("$dir/$file"); print "$file (${ox}x${oy})\n"; print " --> $thumbfile\n"; unless (-e "$dir/$thumbfile" && (stat("$dir/$thumbfile"))[9] > (stat("$dir/$file"))[9]) { print " generating!\n"; system("convert", "-geometry", "100x100", "$dir/$file", "$dir/$thumbfile"); } if (($ox > 800 && $oy > 600) || ($ox > 600 && $oy > 800)) { print " --> $medfile\n"; unless (-e "$dir/$medfile" && (stat("$dir/$medfile"))[9] > (stat("$dir/$file"))[9]) { print " generating!\n"; system("convert", "-geometry", "800x600", "$dir/$file", "$dir/$medfile"); } } else { $medfile = ""; # no medium sized file } my ($w, $h) = imgsize("$dir/$thumbfile"); print " w=$w, h=$h\n"; push @thumbs, { 'full' => $file, 'thumb' => $thumbfile, 'med' => $medfile, 'w' => $w, 'h' => $h }; &make_index(); # so I can press reload while it's running. :) } sub make_index { open (INDEX, ">$dir/index.html"); print INDEX "\r\n"; print INDEX "\r\n"; print INDEX "\r\n"; my $col = 0; foreach my $t (@thumbs) { if ($col > THUMB_COLUMNS) { print INDEX "\r\n"; $col = 0; } $col++; if ($col == 1) { print INDEX "\r\n"; } if ($t->{'med'}) { print INDEX "\r\n"; } else { print INDEX "\r\n"; } } print INDEX "\r\n"; print INDEX "
{'med'}\">{'thumb'}\" width=$t->{'w'} height=$t->{'h'} border=1>
[Med]
{'full'}\">[Large]
$t->{'full'}
{'full'}\">{'thumb'}\" width=$t->{'w'} height=$t->{'h'} border=1>
[Large]

$t->{'full'}
\r\n"; print INDEX "\r\n"; close INDEX; }