Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Perl Subroutine for resizing images
#!/usr/bin/perl
use Image::Magick;
$imagePath = "../images/";
@sizes = ("480", "768", "992", "1200");
#reading sub, takes argument (filename)
sub resizeFiles {
@fileList = @_;
foreach (@fileList) {
print "Reading $_\n";
my $p = new Image::Magick;
my $filename = $_;
$p->Read($filename);
foreach (@sizes) {
my $new = $p->Clone;
$new->Resize(geometry=>$_);
(my $without_extension = $filename) =~ s/\.[^.]+$//;
$new_filename = $without_extension."-".$_.".jpg";
$new->Write(filename => $new_filename, interlace=> plane, quality => 85);
#make @2X for retina
$new->Resize(geometry=>$_*2);
$retina_filename = $without_extension."-".$_."\@2X.jpg";
$new->Write(filename => $retina_filename, interlace=> plane, quality => 85);
}
}
}
chdir($imagePath) or die "Can't chdir to $imagePath";
#run the resizeFiles sub in a loop for maximum effiency. panorama.jpg was just my demo file
resizeFiles("panorama.jpg");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.