#!/usr/local/perl/bin/perl # Script from http://netfactory.dk/technology/perl/graphics/ use strict; use warnings; use Image::Magick; use File::Copy; use File::Find; my $VERSION = 1; # Config my $file_directory = '/home/user/tmp/photos'; my $make_backup = 1; # zero for no backup # The script sub wanted { if (/^.*\.jpg$/i) { my $file = "$File::Find::dir/$_"; File::Copy::copy($file, $file . ".bck") if ($make_backup); # make backup copy imgResize($file); } } find (\&wanted, ,$file_directory); exit(); sub imgResize { my ($file) = @_; my $image = Image::Magick->new(magick=>'JPEG'); my $x = $image->Read($file); my $width = $image->Get('width'); my $height = $image->Get('height'); print STDERR "$file: w($width) h($height)\n"; if (($width < 800) && ($height < 800)) { print STDERR "$file - no resize\n"; } else { if ($width > $height) { my $ratio = (800/$width); my $new_width = 800; my $new_height = $ratio * $height; print STDERR "$file: new w($new_width) h($new_height)\n"; $image->Resize(geometry=>'800x600'); } else { my $ratio = (800/$width); my $new_width = $ratio * $width; my $new_height = 800; print STDERR "$file: new w($new_width) h($new_height)\n"; $image->Resize(geometry=>'600x800'); } open(IMAGE, ">$file"); $image->Write(file=>\*IMAGE, filename=> $file ); close(IMAGE); } }