#!/usr/bin/perl #Basic step is : 1/ store dir 2/ read each file from each dir 3/ resize the file. # --------------------------------------------------------------------------------- # DESC: # # resize.pl resizes original image files under current dir and store it in a new # dir in each image dir. original image file is kept. example: resize.pl is being # run under /root, then this program searches every sub-dir for image file and # resize the image in it. afterwards generates a sub-dir in each working dir to # store the resized image. # # About File Extention # # File's extension will be kept as it is or you can provide a file extension which # keeps every file with the same extention. # # DATE: June 15 2001 # # AUTH: # # James Li MRP509 # # One last notes : You can modify this code in any way but please keep this comment part #------------------------------------------------------------------------------------ use GD; use Cwd; use File::Copy; $curDir = cwd(); @dirList = ("$curDir"); &helper; $width= &promptUser("Enter the expected image width","70"); &quit($width); $heigh= &promptUser("Enter the expected image heigh","100"); &quit($heigh); $quality= &promptUser("Enter the expected image quality score","75"); &quit($heigh); $smallpics= &promptUser("Enter the expected directory name which stores the resized image","smallpics"); &quit($smallpics); # Just a marker for temp file. $preFileName = "allYoungTemp_"; # Just a counter! $x = 0; &storeDir; &resize; #-----------------------storeDir----------------------# # Recursively store all sub-dir and sub...-dir sub storeDir { my $curDir = $dirList[$x]; chdir($curDir); # Store every sub-dir in the current Dir foreach $temp (<*>) { push(@dirList,$curDir."/".$temp) if (-d $temp); } $x++; # Go through every dir store in the @dirList &storeDir if ($x <= $#dirList); } # End of storeDir #-----------------------resize-----------------------# sub resize { my $worDir = shift(@dirList); # Skip the home dir and if no dir under home dir, eixt. if ($worDir eq $curDir) { unless ($worDir = shift(@dirList)) { exit; } } # Change to the dir that we want to work on with. chdir($worDir); # Go through every image and resize it. RESIZE: while (<*.*>) { ($dummy_1,$ext) = split(/\./); # else : Make a flag to decide if we need to call sub moveToSmallPics. Case no image file in current dir, avoid creating useless dir. if ($ext!~ /[jpg|jpeg]/i) { next RESIZE ; } else { $flag = 1; } open (FILE,"$_") || die $!; $image = newFromJpeg GD::Image(FILE); ($w1,$h1) = $image->getBounds(); my($w2,$h2) = &getNewSize($w1,$h1); $tempImage = new GD::Image($w2,$h2) || die $!; # tempImageFile naming rule is : "allYoung_original image file" . we will rename and copy it back to the destinated folder. $tempImageFile = $preFileName.$_; open (TEMP,">$tempImageFile"); $tempImage->copyResized($image,0,0,0,0,$w2,$h2,$w1,$h1); #80 is the default quality score. Change it as you like! $jpegdata = $tempImage->jpeg($quality); binmode TEMP; print TEMP $jpegdata; close FILE; close TEMP; } # End of RESIZE # if resized has happened ,move resized images to '$smallpics' &moveToSmallPics($worDir) if ($flag); # if there are more dir,start over the resize. if (@dirList) { &resize; }else { print "Done! Press enter to quit\n"; my $dummy_2 = ; exit 1; } } #-----------------------getNewSize----------------------# # Calculate the thumbnail dimention. It keeps the original image dimention ratio. sub getNewSize { my($w2,$h2) = @_; my $tempHeigh = ($width/$w2)*$h2; my $tempWidth = ($heigh/$h2)*$w2; $total_1 = $width + $tempHeigh; $total_2 = $heigh + $tempWidth; if ($total_1 >= $total_2 ) { $w2 = $tempWidth; $h2 = $heigh; } else { $w2 = $width; $h2 = $tempHeigh; } return $w2,$h2; } #-----------------------moveToSmallPics----------------# sub moveToSmallPics { mkdir $smallpics; my $worDir = $_[0]; while (<$preFileName*.*>) { $newName = $_; $newName =~ s/$preFileName//; $dst = $worDir."/".$smallpics."/".$newName; copy($_,$dst); unlink $_; } } #-----------------------promptUser-------------------# sub promptUser { local($promptString,$defaultValue) = @_; if ($defaultValue) { print $promptString, " [", $defaultValue, "] : "; } else { print $promptString, ": "; } chomp($answer = ); # If no user input value, then take default value, take user input otherwise if ("$defaultValue") { return $answer ? $answer : $defaultValue; # return $answer if it has a value } else { return $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 | This program resizes image file base on dimension user's input |\n"; print "\t | and stores the resized image to certain directory which located|\n"; print "\t | in the same directory where the original image is. |\n"; print "\t | |\n"; print "\t | BE WARE |\n"; print "\t | This program takes effect to every image file in certain dir |\n"; print "\t | which is located in the dir where the resize.pl file is. |\n"; print "\t | |\n"; print "\t | All files will be overwrited if the file with the same name |\n"; print "\t | exist. |\n"; print "\t | |\n"; print "\t | To be fixed soon: |\n"; print "\t | Do not rerun the app as one sub-dir will be created again! |\n"; print "\t | |\n"; print "\t | In the prompt line words between \"[\" \"]\" are the default value |\n"; print "\t | |\n"; print "\t | TO quit the program without any input enter \"q\" at anytime. |\n"; print "\t | |\n"; print "\t ----------------------------++ James ++---------------------------\n"; print "\n"; } 1;