Skip to content

Instantly share code, notes, and snippets.

@geetotes
Created March 26, 2014 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geetotes/9790951 to your computer and use it in GitHub Desktop.
Save geetotes/9790951 to your computer and use it in GitHub Desktop.
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