Skip to content

Instantly share code, notes, and snippets.

@tokuhirom
Created April 23, 2009 02:41
Show Gist options
  • Save tokuhirom/100249 to your computer and use it in GitHub Desktop.
Save tokuhirom/100249 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use autodie;
use Getopt::Long;
use Imager;
use GD;
use Image::Magick;
my $xsize = 240;
my $ysize = 180;
GetOptions(
'xsize' => \$xsize,
'ysize' => \$ysize,
) or die;
my $srcfile = shift @ARGV or die "Usage: $0 foo.jpg\n";
print "# Imager $Imager::VERSION\n";
print "# GD $GD::VERSION\n";
print "# Image::Magick $Image::Magick::VERSION\n";
for my $qtype (qw/normal mixing preview/) {
imager($qtype);
}
for my $method (qw/copyResized copyResampled/) {
gd($method);
}
imagemagick();
# Imager ver.
# see Imager::Transformations
sub imager {
my $qtype = shift or die; # normal, mixing, preview is available.
my $img = Imager->new;
$img->read(file => $srcfile) or die;
my $scaled = $img->scale(xpixels => $xsize, ypixels => $xsize, qtype => $qtype) or die;
$scaled->write(file => "imager-$qtype.jpg", type => 'jpeg') or die;
$scaled->write(file => "imager-$qtype.png", type => 'png') or die;
}
sub gd {
my $method = shift or die; # copyResized or copyResampled
my $gd = GD::Image->new($srcfile) or die;
my $scaled = GD::Image->new($xsize, $xsize);
$scaled->$method( $gd, 0,0,0,0, $xsize,$xsize, $gd->width,$gd->height);
open my $fh, '>', "gd-$method.jpg";
print $fh $scaled->jpeg;
close $fh;
}
sub imagemagick {
my $img = Image::Magick->new;
$img->Read($srcfile);
my ($width, $height) = $img->Get('width', 'height');
$img->Resize(
width => $xsize,
height => $xsize,
);
$img->Write('imagemagick.jpg');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment