#!/usr/bin/perl -w # # Objective : resize every image files located in first level of sub dir. A dir # which is used to store the thumbnails will be created afterwards. # Supported image type: jpg|jpeg|gif|png|bmp # # Note: depth of dir can be controled by doing something like this # $File::Find::prune = 1 if $File::Find::name =~ tr/\/// >= $depth; use strict; use Image::Magick; use File::Find; &helper; my $width= &promptUser("Enter the expected image width","80"); &quit($width); my $height= &promptUser("Enter the expected image heigh","120"); &quit($height); my $quality= &promptUser("Enter the expected image quality","75"); &quit($quality); my $tNails= &promptUser("Enter the expected thumbnails' directory","smallpics"); &quit($tNails); my $dir = "."; print "resizing image...\n"; find(\&resize,$dir); print "Process done!\n"; exit; #----------------------resize------------------# # traverse every dir and resize each image file sub resize { my $name = $File::Find::name; # skip dir where script resides return if $File::Find::dir eq $dir and !(-d $name); # skip dir which has "ThumbNails" dircreated.. if (-d "$name/$tNails") { print "skipping $name\n"; $File::Find::prune = 1; return; } return unless /\.(jpg|jpeg|gif|png|bmp)$/i; my $image = Image::Magick->new; $image->Read($_); my ($w,$h) = $image->Get('width','height'); return if ($w <= $width || $h <= $height); mkdir $tNails or die "Making dir failed:$!" unless (-e $tNails); ($w,$h) = &getNewSize($w,$h); $image->Resize(width=>$w,height=>$h); # Remove image profile(IPTC/ICM).identify -verbose image to see # it's actuall profile type and size of it. $image->Profile(profile=>"IPTC"); #$image->Profile(profile=>"ICM"); # $image->Contrast(sharpen=>"true"); enhance or reduce the image contrast $image->Set(quality=>$quality); $image->Write(filename=>"$tNails/$_"); print "\t$name\n"; } #----------------------getNewSize------------------# # Calculates image dimention ratio. sub getNewSize { my ($w,$h) = @_; my ($newW,$newH)=($width,$height); if ($w > $h) { my $tmp = $width; $newW = $height; $newH = $tmp; } my $tempHeight = ($newW/$w)*$h; my $tempWidth = ($newH/$h)*$w; my $total_1 = $newW + $tempHeight; my $total_2 = $newH + $tempWidth; if ($total_1 >= $total_2 ) { ($w,$h) = ($tempWidth,$newH); } else { ($w,$h) = ( $newW,$tempHeight); } } #--------------------promptUser--------------------# sub promptUser { my($promptString,$defaultValue) = @_; if ($defaultValue) { print $promptString, " [", $defaultValue, "] : "; } else { print $promptString, ": "; } chomp(my $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; __END__