#!/usr/bin/perl ########################## #$Id: makethumbs 58 2003-02-17 13:14:36Z steve $ #$Log$ #Revision 1.2 2003/02/17 13:14:36 steve #added better system() handling # #Revision 1.1 2002/12/13 21:08:26 steve #Initial revision # ########################## use strict; use Getopt::Std; getopts("Rhfnc"); our( $opt_R, $opt_f, $opt_n, $opt_h, $opt_c ); my $thumbs = ".thumbs"; my @formats = qw( jpg jpeg gif png ); usage() if defined $opt_h; my $recurse = defined $opt_R; my $force = defined $opt_f; my $dry_run = defined $opt_n; my $clean = defined $opt_c; print "doing a dry run\n" if $dry_run; mkthumbs( $ENV{'PWD'} , $force, $recurse, $dry_run ); sub mkthumbs{ my ($dir, $force, $recurse, $dry_run) = @_; my $thumb_dir = "$dir/$thumbs"; my @files = (); # print "changing dir to '$dir'\n"; # chdir "$dir" or die "cannot chdir to $dir: $!"; print "processing $dir...\n"; my $fmtrex = ".".join("\$|\\.", @formats)."\$"; print "opening '$dir'\n"; my $dir_h; opendir $dir_h, "$dir" or die "cannot open $dir: $!"; my @dir_list = readdir($dir_h); close $dir_h; foreach my $file (@dir_list){ if( -d "$dir/$file" ){ # recurse! # print "directory '$file'..."; if ($file ne $thumbs && ($file !~ /^\./ )&& $recurse){ mkthumbs( "$dir/$file", $force, $recurse, $dry_run); }else{ # print "\n"; } }elsif( $file =~ /$fmtrex/i ){ push @files, "$file"; } } print "No images found in \"$dir\". Skipping directory\n" if( !@files ); unless( -d $thumb_dir || !@files ){ print "making \"$thumb_dir\"\n"; unless( $dry_run ){ mkdir "$thumb_dir" or die "cannot make directory $thumb_dir: $!"; } } foreach my $file ( @files ){ my $outfile = "$thumb_dir/$file"; # if the thumbnail doesn't exist, is older than the picture if( !-e $outfile || (-M $outfile > -M "$dir/$file" ) || $force ){ print "Scaling \"$dir/$file\" and saving in \"$outfile\"\n"; unless($dry_run){ (system( "convert", "-scale", "100x100", "$dir/$file", $outfile ) == 0) or die "\n"; } } } if( $clean ){ print "Cleaning out stale thumbnails...\n"; opendir THUMB, "$thumb_dir" or die "cannot open directory $thumb_dir: $!"; foreach my $file ( readdir THUMB ){ unless( $file =~ /^\./ || -e "$dir/$file" ){ print "Removing $thumb_dir/$file\n"; unlink "$thumb_dir/$file"; } } print "done cleaning.\n"; } return; } sub usage{ my $filfmt = join ", ", @formats; die< USAGE }