$newimg = $img->scale(xpixels=>400);
$newimg = $img->scale(xpixels=>400, ypixels=>400);
- $newimg = $img->scale(xpixels=>400, ypixels=>400, type=>min);
+ $newimg = $img->scale(xpixels=>400, ypixels=>400, type=>'min');
$newimg = $img->scale(scalefactor=>0.25);
+ $newimg = $img->scaleX(pixels=>400);
+ $newimg = $img->scaleX(scalefactor=>0.25);
+ $newimg = $img->scaleY(pixels=>400);
+ $newimg = $img->scaleY(scalefactor=>0.25);
+
$newimg = $img->crop(left=>50, right=>100, top=>10, bottom=>100);
$newimg = $img->crop(left=>50, top=>10, width=>50, height=>90);
$dest->paste(left=>40,top=>20,img=>$logo);
- $img->rubthrough(src=>$srcimage,tx=>30,ty=>50);
+ $img->rubthrough(src=>$srcimage,tx=>30, ty=>50);
+ $img->rubthrough(src=>$srcimage,tx=>30, ty=>50,
+ src_minx=>20, src_miny=>30,
+ src_maxx=>20, src_maxy=>30);
$img->flip(dir=>"h"); # horizontal flip
To scale an image so porportions are maintained use the
C<$img-E<gt>scale()> method. if you give either a xpixels or ypixels
parameter they will determine the width or height respectively. If
-both are given the one resulting in a larger image is used. example:
-C<$img> is 700 pixels wide and 500 pixels tall.
+both are given the one resulting in a larger image is used, unless you
+set the C<type> parameter to C<'min'>. example: C<$img> is 700 pixels
+wide and 500 pixels tall.
$newimg = $img->scale(xpixels=>400); # 400x285
$newimg = $img->scale(ypixels=>400); # 560x400
$newimg = $img->scale(xpixels=>400,ypixels=>400); # 560x400
- $newimg = $img->scale(xpixels=>400,ypixels=>400,type=>min); # 400x285
+ $newimg = $img->scale(xpixels=>400,ypixels=>400,type=>'min'); # 400x285
$newimg = $img->scale(scalefactor=>0.25); 175x125
$newimg = $img->scale(); # 350x250
variations and the scaled image is by more than 3-5 times smaller than
the original.
+=over
+
+=item *
+
+xpixels, ypixels - desired size of the scaled image. The resulting
+image is always scaled proportionally. The C<type> parameter controls
+whether the larger or smaller of the two possible sizes is chosen.
+
+=item *
+
+type - controls whether the larger or smaller of the two possible
+sizes is chosen, possible values are:
+
+=over
+
+=item *
+
+min - the smaller of the 2 sizes are chosen.
+
+=item *
+
+max - the larger of the 2 sizes. This is the default.
+
+=back
+
+The behaviour when C<type> is set to some other value is undefined.
+
+For example, if the original image is 400 pixels wide by 200 pixels
+high and C<xpixels> is set to 300, and C<ypixels> is set to 160. When
+C<type> is C<'min'> the resulting image is 300 x 150, when C<type> is
+C<'max'> the resulting image is 320 x 150.
+
+C<type> is only used if both C<xpixels> and C<ypixels> are supplied.
+
+=item *
+
+qtype - defines the quality of scaling performed. Possible values are:
+
+=over
+
+=item *
+
+normal - high quality scaling. This is the default.
+
+=item *
+
+preview - lower quality.
+
+=back
+
+=back
+
If you need to scale images per axis it is best to do it simply by
-calling scaleX and scaleY. You can pass either 'scalefactor' or
+calling scaleX() or scaleY(). You can pass either 'scalefactor' or
'pixels' to both functions.
+Returns the scaled image on success.
+
+Returns false on failure, check the errstr() method for the reason for
+failure.
+
+=item scaleX
+
+scaleX() will scale along the X dimension, changing the width of the
+image:
+
+ $newimg = $img->scaleX(pixels=>400); # 400x500
+ $newimg = $img->scaleX(scalefactor=>0.25) # 175x500
+
+=item scaleY
+
+scaleY() will scale along the Y dimension, changing the height of the
+image:
+
+ $newimg = $img->scaleY(pixels=>400); # 700x400
+ $newimg = $img->scaleY(scalefactor=>0.25) # 700x125
+
=item crop
Another way to resize an image is to crop it. The parameters to
where the right and bottom edges are non-inclusive. If a parameter is
omitted a default is used instead.
- # the first two produce the same image
+The possible parameters are:
+
+=over
+
+=item *
+
+C<left> - the left edge of the area to be cropped. Default: 0
+
+=item *
+
+C<top> - the top edge of the area to be cropped. Default: 0
+
+=item *
+
+C<right> - the right edge of the area to be cropped. Default: right
+edge of image.
+
+=item *
+
+C<bottom> - the bottom edge of the area to be cropped. Default:
+bottom edge of image.
+
+=item *
+
+C<width> - width of the crop area. Ignored if both C<left> and C<right> are
+supplied. Centered on the image if neither C<left> nor C<right> are
+supplied.
+
+=item *
+
+C<height> - height of the crop area. Ignored if both C<top> and
+C<bottom> are supplied. Centered on the image if neither C<top> nor
+C<bottom> are supplied.
+
+=back
+
+For example:
+
+ # these produce the same image
$newimg = $img->crop(left=>50, right=>100, top=>10, bottom=>100);
$newimg = $img->crop(left=>50, top=>10, width=>50, height=>90);
- $newimg = $img->crop(left=>50, right=>100); # top
+ $newimg = $img->crop(right=>100, bottom=>100, width=>50, height=>90);
+
+ # and the following produce the same image
+ $newimg = $img->crop(left=>50, right=>100);
+ $newimg = $img->crop(left=>50, right=>100, top=>0,
+ bottom=>$img->getheight);
+
+ # grab the top left corner of the image
+ $newimg = $img->crop(right=>50, bottom=>50);
You can also specify width and height parameters which will produce a
new image cropped from the center of the input image, with the given
$newimg = $img->crop(width=>50, height=>50);
-The width and height parameters take precedence over the left/right
-and top/bottom parameters respectively.
+If you supply C<left>, C<width> and C<right> values, the C<right>
+value will be ignored. If you supply C<top>, C<height> and C<bottom>
+values, the C<bottom> value will be ignored.
+
+The edges of the cropped area default to the edges of the source
+image, for example:
+
+ # a vertical bar from the middle from top to bottom
+ $newimg = $img->crop(width=>50);
+
+ # the right half
+ $newimg = $img->crop(left=>$img->getwidth() / 2);
+
+If the resulting image would have zero width or height then crop()
+returns false and $img->errstr is an appropriate error message.
=item rotate
my $rotpi4 = $img->rotate(radians=>3.14159265/4);
Exact image rotation uses the same underlying transformation engine as
-the matrix_transform() method.
+the matrix_transform() method (see Imager::Engines).
+
+You can also supply a C<back> argument which acts as a background
+color for the areas of the image with no samples available (outside
+the rectangle of the source image.) This can be either an
+Imager::Color or Imager::Color::Float object. This is B<not> mixed
+transparent pixels in the middle of the source image, it is B<only>
+used for pixels where there is no corresponding pixel in the source
+image.
To rotate in steps of 90 degrees, use the 'right' parameter:
Rotations are clockwise for positive values.
+
=back
put 'over' the other with a certain amount of opaqueness. The
method that does this is rubthrough.
- $img->rubthrough(src=>$srcimage,tx=>30,ty=>50);
+ $img->rubthrough(src=>$overlay,
+ tx=>30, ty=>50,
+ src_minx=>20, src_miny=>30,
+ src_maxx=>20, src_maxy=>30);
-That will take the image C<$srcimage> and overlay it with the upper
-left corner at (30,50). You can rub 2 or 4 channel images onto a 3
-channel image, or a 2 channel image onto a 1 channel image. The last
-channel is used as an alpha channel.
+That will take the sub image defined by I<$overlay> and
+I<[src_minx,src_maxx)[src_miny,src_maxy)> and overlay it on top of
+I<$img> with the upper left corner at (30,50). You can rub 2 or 4
+channel images onto a 3 channel image, or a 2 channel image onto a 1
+channel image. The last channel is used as an alpha channel. To add
+an alpha channel to an image see I<convert()>.
=item flip
$new = $img->convert(matrix=>[ [ 0.333, 0.333, 0.334 ] ])
+Convert a 2 channel image (grayscale with alpha) to an RGBA image with
+the grey converted to the specified RGB color:
+
+ # set (RGB) scaled on the grey scale portion and copy the alpha
+ # channel as is
+ my $colored = $gray->convert(matrix=>[ [ ($red/255), 0 ],
+ [ ($green/255), 0 ],
+ [ ($blue/255), 0 ],
+ [ 0, 1 ],
+ ]);
=head2 Color Mappings
$img->map(maps=>[\@redmap, [], \@bluemap]);
-
-
-
-
+=cut