#example usage: # make_thumbnail_IM('/home/me/i.jpg', '/home/me/i2.jpg', 80, 'square', 90); # will make a 80x80 thumbnail (cropped) quality 90 for picture home/me/i.jpg, and save it to /home/me/i2.jpg sub make_thumbnail_IM{ # receives: # file source (absolute path on machine) # file target out (absolute path on machine) # restriction (pixels) # restriction type # quality my ($src,$out,$restriction,$restrict_type,$quality)=@_; # both restrict to height and width :both won't go over this.. # width restrict to width # height restrict to height # square square - will crop sides or top and bottom to fit. neat. my $img = Image::Magick->new; #blank $img->Read($src); #read source file into blank my ($w,$h) = $img->Get('width','height'); # find dimensions # # # #if width is bigger then height, we use restrict to w if ($restrict_type eq 'both'){ if ($w>$h){ $restrict_type='width'; } else { $restrict_type='height'; } } my $newh; my $neww; # initialize new dimensions # not used if 'square' if ($restrict_type eq 'height') { $neww=(($w*$restriction)/$h); $img->Resize(width=>$neww, height=>$restriction); } if ($restrict_type eq 'width') { $newh=(($h*$restriction)/$w); $img->Resize(width=>$restriction, height=>$newh); } if ($restrict_type eq 'square'){ # my ($cut,$xcut,$ycut); if ($w>$h){ $cut=$h; $xcut=(($w-$h)/2); $ycut=0; } if ($w<$h){ $cut=$w; $xcut=0; $ycut=(($h-$w)/2); } $img->Crop(width=>$cut,height=>$cut,x=>$xcut,y=>$ycut); $img->Resize(width=>$restriction, height=>$restriction); # } $img->Set(quality=>$quality); $img->Write($out); undef $img; }