elsif ($opts{ypixels}) {
$opts{scalefactor}=$opts{ypixels}/$self->getheight();
}
+ elsif ($opts{constrain} && ref $opts{constrain}
+ && $opts{constrain}->can('constrain')) {
+ # we've been passed an Image::Math::Constrain object or something
+ # that looks like one
+ (undef, undef, $opts{scalefactor})
+ = $opts{constrain}->constrain($self->getwidth, $self->getheight);
+ unless ($opts{scalefactor}) {
+ $self->_set_error('constrain method failed on constrain parameter');
+ return undef;
+ }
+ }
if ($opts{qtype} eq 'normal') {
$tmp->{IMG}=i_scaleaxis($self->{IMG},$opts{scalefactor},0);
=item *
+constrain - an Image::Math::Constrain object defining the way in which
+the image size should be constrained.
+
+=item *
+
+scalefactor - if none of xpixels, ypixels or constrain is supplied
+then this is used as the ratio to scale by. Default: 0.5.
+
+=item *
+
type - controls whether the larger or smaller of the two possible
sizes is chosen, possible values are:
Returns false on failure, check the errstr() method for the reason for
failure.
+ # setup
+ my $image = Imager->new;
+ $image->read(file => 'somefile.jpg')
+ or die $image->errstr;
+
+ # all full quality unless indicated otherwise
+ # half the size:
+ my $half = $image->scale;
+
+ # double the size
+ my $double = $image->scale(scalefactor => 2.0);
+
+ # so a 400 x 400 box fits in the resulting image:
+ my $fit400x400inside = $image->scale(xpixels => 400, ypixels => 400);
+ my $fit400x400inside2 = $image->scale(xpixels => 400, ypixels => 400,
+ type=>'max');
+
+ # fit inside a 400 x 400 box
+ my $inside400x400 = $image->scale(xpixels => 400, ypixels => 400,
+ type=>'min');
+
+ # make it 400 pixels wide or high
+ my $width400 = $image->scale(xpixels => 400);
+ my $height400 = $image->scale(ypixels => 400);
+
+ # low quality scales:
+ # to half size
+ my $low = $image->scale(qtype => 'preview');
+
+ # using an Image::Math::Constrain object
+ use Image::Math::Constrain;
+ my $constrain = Image::Math::Constrain->new(800, 600);
+ my $scaled = $image->scale(constrain => $constrain);
+
+ # same as Image::Math::Constrain version
+ my $scaled2 = $image->scale(xpixels => 800, ypixels => 600, type => 'min');
+
=item scaleX
scaleX() will scale along the X dimension, changing the width of the
#!perl -w
use strict;
use lib 't';
-use Test::More tests => 22;
+use Test::More tests => 46;
BEGIN { use_ok(Imager=>':all') }
is($im->errstr, "invalid value for type parameter", "check error message");
}
+SKIP:
+{ # Image::Math::Constrain support
+ eval "require Image::Math::Constrain;";
+ $@ and skip "module optional Image::Math::Constrain not installed", 3;
+ my $constrain = Image::Math::Constrain->new(20, 100);
+ my $im = Imager->new(xsize => 160, ysize => 96);
+ my $result = $im->scale(constrain => $constrain);
+ ok($result, "successful scale with Image::Math::Constrain");
+ is($result->getwidth, 20, "check result width");
+ is($result->getheight, 12, "check result height");
+}
+
+{ # scale size checks
+ my $im = Imager->new(xsize => 160, ysize => 96); # some random size
+
+ scale_test($im, 80, 48, "48 x 48 def type",
+ xpixels => 48, ypixels => 48);
+ scale_test($im, 80, 48, "48 x 48 max type",
+ xpixels => 48, ypixels => 48, type => 'max');
+ scale_test($im, 80, 48, "80 x 80 min type",
+ xpixels => 80, ypixels => 80, type => 'min');
+ scale_test($im, 80, 48, "no scale parameters (default to 0.5 scalefactor)");
+ scale_test($im, 120, 72, "0.75 scalefactor",
+ scalefactor => 0.75);
+ scale_test($im, 80, 48, "80 width",
+ xpixels => 80);
+ scale_test($im, 120, 72, "72 height",
+ ypixels => 72);
+}
+
+sub scale_test {
+ my ($in, $exp_width, $exp_height, $note, @parms) = @_;
+
+ print "# $note: @parms\n";
+ SKIP:
+ {
+ my $scaled = $in->scale(@parms);
+ ok($scaled, "scale $note")
+ or skip("failed to scale", 2);
+ is($scaled->getwidth, $exp_width, "check width");
+ is($scaled->getheight, $exp_height, "check height");
+ }
+}