#!/usr/bin/env perl ###################################################################### # make_thumbNails.pl # Krister Bruhwel # April 2007 # # This script needs the "convert" program to function. # This script assumes all pictures are jpegs ... :( hey, not my bag # ###################################################################### use File::Find; use File::Path; use File::Copy; use Getopt::Long; GetOptions("help", "h", "w"); #take care of command line flags and set defaults if ($opt_help) { printHelp(); exit(); } if ($opt_h) { printHelp(); exit(); } if ($opt_w) { $wipe = 1} else { $wipe = 0 } #make_thumbs(); $max_depth = 1; $size = "120x120"; #size of thumbs if ($wipe) { start_fresh(); } else { make_thumbs(); } sub start_fresh { print "starting fresh ... removing old thumb pics\n"; find({preprocess => \&preprocess, wanted => \&getDirNames}, "."); #get rid of duplicates undef @in; @dir_list = sort grep(!$in{$_}++, @dirNames_long); #get rid of "." shift(@dir_list); foreach $dirName (@dir_list) { print "removing thumb files from $dirName\n"; #capture CTRL-C $SIG{INT} = sub { exit(0); }; chdir($dirName); rmtree thumb_pics; unlink "thumb_index.html"; unlink "master_thumb_index.html"; chdir(".."); } print "old thumb files removed.\n"; } sub make_thumbs { $time = localtime(); $number_of_thumbs = 0; find({preprocess => \&preprocess, wanted => \&getDirNames}, "."); #get rid of duplicates undef @in; @dir_list = sort grep(!$in{$_}++, @dirNames_long); #get rid of "." shift(@dir_list); foreach $dirName (@dir_list) { #capture CTRL-C $SIG{INT} = sub { exit(0); }; chdir($dirName); unless (opendir(TMP, $thumb_pics)) { mkdir thumb_pics; } else { closedir(TMP); } unlink "thumb_index.html"; undef(@list_of_jpgs); @list_of_jpgs = `find . -maxdepth 1 -name "*.JPG" -o -name "*.jpg"`; chomp(@list_of_jpgs); #foreach $l (@ret) { for ($i=0; $i<=$#list_of_jpgs; $i++) { $list_of_jpgs[$i] =~ s/\.\///; #print "$list_of_jpgs[$i]"; } for ($i= 0; $i <= $#list_of_jpgs; $i++) { $pic = $list_of_jpgs[$i]; $pic =~ s/\.JPG//; $pic =~ s/\.jpg//; $pic = "thumb_pics/thumb_$pic.jpg"; if (-e $pic) { print "skipping -> $dirName/$list_of_jpgs[$i]\n"; } else { print "thumbing -> $dirName/$list_of_jpgs[$i]\n"; $cmd = "convert \"$list_of_jpgs[$i]\" -resize $size \"$pic\""; #print "$cmd\n"; system($cmd)==0 or die "Error:\ndir: $dirNames\ncmd:$cmd\n"; $number_of_thumbs++; } } make_html_index(); chdir(".."); } #chdir(".."); make_master_html_index(); print "total thumbnail pics made: $number_of_thumbs\n"; print "program start: $time\n"; $time = localtime(); print "program end: $time\n"; } # Subroutines # ##################################################################### # stop find at a given depth sub preprocess { my $depth = $File::Find::dir =~ tr[/][]; return @_ if $depth < $max_depth; return grep { not -d } @_ if $depth == $max_depth; return; } sub make_master_html_index { open FILE, ">master_thumb_index.html" or die "cant write master html\n"; print FILE "\n"; print FILE "\n"; print FILE "