#!/usr/bin/perl # Basic step : 1/ store dirs 2/ resize image file stored in each dir. # --------------------------------------------------------------------------------- # DESC: # # resize.pl resizes image files under child-dir and store it in a new dir created # under current dir. original image file is kept. # # About File Extention # # File's extension will be kept as it is.Currently image format supported are jpg/jpeg # gif,bmp,png.More are available I refer you to www.imagemagick.org # # DATE: May 10 2001 (Revised on Oct 10 2001) # # AUTH: # # James, Li MRP509 # # One last notes : You can modify this code in any way but please keep this comment part #------------------------------------------------------------------------------------ use Image::Magick; use Cwd; &helper; $width= &promptUser("Enter the expected image width","70"); &quit($width); $height= &promptUser("Enter the expected image heigh","100"); &quit($height); $quality= &promptUser("Enter the expected image quality score","75"); &quit($quality); $tNails= &promptUser("Enter the expected thumbnails' directory","ThumbNails"); &quit($tNails); my @dirList=(); &storeDir; &resize; #---------------------storeDir---------------------# # Stores all child-dirs. sub storeDir { my $curDir = cwd(); chdir($curDir) or die "changing to the dir failed: $!\n"; foreach my $temp (<*>) { push(@dirList,$curDir."/".$temp) if (-d $temp); } } #----------------------resize----------------------# sub resize { my $worDir = shift @dirList; &makeThumbnails($worDir); if (@dirList) { &resize; } else { print "Done! Press enter to quit\n"; my $dummy = ; exit 0; } } #------------------makeThumbnail-------------------# sub makeThumbnails { chdir($_[0]) or die "cannot change to $_[0]: $!"; exit 0 if (-e $tNails); RESIZE: while (<*.*>) { my ($dummy,$ext) = split(/\./); if ($ext!~ /[jpg|jpeg|gif|png|bmp]/i) { next RESIZE ; } else { mkdir $tNails unless (-e $tNails); } print "resizing image $_\n"; #operation on image starts here. $image = Image::Magick->new; $image->Read($_); my ($w,$h) = $image->Get('width','height'); ($w,$h) = &getNewSize($w,$h); $image->Resize(width=>$w,height=>$h); $image->Write(filename=>"$tNails/$_",quality=>$quality); } } #----------------------getNewSize------------------# # Calculates image dimention ratio. sub getNewSize { my ($w,$h) = @_; my $tempHeight = ($width/$w)*$h; my $tempWidth = ($height/$h)*$w; my $total_1 = $width + $tempHeight; my $total_2 = $height + $tempWidth; if ($total_1 >= $total_2 ) { ($w,$h) = ($tempWidth,$height); } else { ($w,$h) = ( $width,$tempHeight); } } #--------------------promptUser--------------------# sub promptUser { my($promptString,$defaultValue) = @_; if ($defaultValue) { print $promptString, " [", $defaultValue, "] : "; } else { print $promptString, ": "; } chomp($answer = ); if ($defaultValue) { $answer ? $answer : $defaultValue; } else { $answer; } } #-----------------------quit-----------------------# sub quit { exit 1 if ($_[0] =~ /^q/i); } #-----------------------helper---------------------# sub helper { print "\n"; print "\t ----------------------------++ DESC ++---------------------------\n"; print "\t | |\n"; print "\t | What does it do ? |\n"; print "\t | Resizes images located in child-dir |\n"; print "\t | Stores them under the image file directory |\n"; print "\t | Original image files get kept and NO overwrite |\n"; print "\t | User provides expected image dimention,default[*] otherwise. |\n"; print "\t | |\n"; print "\t | To quit the program enter \"q\" at anytime. |\n"; print "\t | |\n"; print "\t ----------------------------++ James ++---------------------------\n"; print "\n"; } 1;