#!/usr/bin/perl -w use strict; use Image::Magick; use Getopt::Std; getopts('vw:h:p:x:c:'); our($opt_v,$opt_w,$opt_h,$opt_p,$opt_x,$opt_c); my $columns = $opt_c || 5; my $thumb_width = $opt_w || 150; my $thumb_height = $opt_h || 150; my $thumb_prefix = $opt_p || '__'; my $thumb_suffix = $opt_x; my @thumb_info; push(@ARGV,'.') unless (@ARGV); for my $dir (@ARGV) { opendir(DIR,$dir) || die "can't open dir $dir: $!"; for my $file (sort readdir DIR) { next unless ($file =~ /\.(gif|jpg|jpeg|png)$/i); my $thumb_file; if (defined $thumb_suffix) { next if ($file =~ /$thumb_suffix$/); $thumb_file = "$file$thumb_suffix"; } else { next if ($file =~ /^$thumb_prefix/); $thumb_file = "$thumb_prefix$file"; } warn "creating thumb: $thumb_file\n" if ($opt_v); my %info = make_thumb($dir, $file, $thumb_file, $thumb_height, $thumb_width); push(@thumb_info,\%info); } } # ugly html in my code! print <Contact Sheet HEAD my $column_num = 0; for my $rh_info (@thumb_info) { my %info = %$rh_info; if ($column_num == 0) { print ""; } print <
$info{filename}
( $info{width} x $info{height} ) CELL $column_num++; if ($column_num == $columns) { print "
"; $column_num = 0; } } print < TAIL sub make_thumb { # adapted from Bernie Porter's photo_album.cgi at # http://www.tildebernie.com/~bernie/cgi/photo_album.txt my ($img_dir, $img_file, $thumb_file, $height, $width) = @_; my $err; ### read in the image and current size my $image = Image::Magick->new; $err = $image->Read("$img_dir/$img_file"); die "**Read error: $err on $image" if $err; my $img_width = $image->Get('width'); my $img_height = $image->Get('height'); ### calculate the thumbnail dimensions my $thumb_height; my $thumb_width; if ($img_height > $height) { my $ratio = $height/$img_height; $thumb_height = $height; $thumb_width = $img_width * $ratio; if ($thumb_width > $width) { my $ratio = $width/$thumb_width; $thumb_width = $width; $thumb_height = $thumb_height * $ratio; } } elsif ($img_width > $width) { my $ratio = $width/$img_width; $thumb_width = $width; $thumb_height = $img_height * $ratio; } else { $thumb_height = $img_height; $thumb_width = $img_width; } ### scale and write $image->Scale( width => $thumb_width, height => $thumb_height); $err = $image->Write(filename=>"$img_dir/$thumb_file"); warn "**Write error: $err on $img_dir/$thumb_file" if $err; return ( thumbname => "$img_dir/$thumb_file", filename => "$img_dir/$img_file", width => $img_width, height => $img_height, ); } sub usage { warn < -h [-p ] [-x ] [-c columns] directories USE }