NAME
Math::Image::CalcResized - Calculate dimensions of image/video resized by ImageMagick-like geometry specification
VERSION
This document describes version 0.006 of Math::Image::CalcResized (from Perl distribution Math-Image-CalcResized), released on 2024-08-29.
FUNCTIONS
calc_image_resized_size
Usage:
calc_image_resized_size(%args) -> [$status_code, $reason, $payload, \%result_meta]
Given size of an image (in WxH, e.g. "2592x1944") and ImageMagick-like resize instruction (e.g. "1024p>"), calculate new resized image.
Examples:
no resizing:
calc_image_resized_size(size => "2592x1944", resize => ""); # -> [200, "OK (envelope generated)", "2592x1944"]scale (down) to 20%:
calc_image_resized_size(size => "2592x1944", resize => "20%"); # -> [200, "OK (envelope generated)", "518x388"]scale (down) width to 20% but height to 40%:
calc_image_resized_size(size => "2592x1944", resize => "20%x40%");Result:
[200, "OK (envelope generated)", "518x777"]scale (down) width to 20% but height to 40% (first percent sign is optional):
calc_image_resized_size(size => "2592x1944", resize => "20x40%");Result:
[200, "OK (envelope generated)", "518x777"]set width to 1024px:
calc_image_resized_size(size => "2592x1944", resize => 1024); # -> [200, "OK (envelope generated)", "1024x768"]shrink width to 1024px:
calc_image_resized_size(size => "2592x1944", resize => "1024>");Result:
[200, "OK (envelope generated)", "1024x768"]shrink width to 10240px (no effect since width is already less than 10240px):
calc_image_resized_size(size => "2592x1944", resize => "10240>");Result:
[200, "OK (envelope generated)", "2592x1944"]enlarge width to 1024px (no effect since width is already greater than 1024px:
calc_image_resized_size(size => "2592x1944", resize => "1024^");Result:
[200, "OK (envelope generated)", "2592x1944"]enlarge width to 10240px:
calc_image_resized_size(size => "2592x1944", resize => "10240^");Result:
[200, "OK (envelope generated)", "10240x7680"]set height to 1024px:
calc_image_resized_size(size => "2592x1944", resize => "x1024");Result:
[200, "OK (envelope generated)", "1365x1024"]shrink height to 768px:
calc_image_resized_size(size => "2592x1944", resize => "x768>");Result:
[200, "OK (envelope generated)", "1024x768"]shrink height to 7680px (no effect since height is already less than 7680px):
calc_image_resized_size(size => "2592x1944", resize => "x7680>");Result:
[200, "OK (envelope generated)", "2592x1944"]enlarge height to 768px (no effect since height is already greater than 768px):
calc_image_resized_size(size => "2592x1944", resize => "x768^");Result:
[200, "OK (envelope generated)", "2592x1944"]enlarge height to 7680px:
calc_image_resized_size(size => "2592x1944", resize => "x7680^");Result:
[200, "OK (envelope generated)", "10240x7680"]fit image inside 20000x10000 (no effect since it already fits):
calc_image_resized_size(size => "2592x1944", resize => "20000x10000");Result:
[200, "OK (envelope generated)", "2592x1944"]fit image inside 20000x1000 (height is reduced to 1000 to make the image fit):
calc_image_resized_size(size => "2592x1944", resize => "20000x1000");Result:
[200, "OK (envelope generated)", "1333x1000"]fit image inside 100x200:
calc_image_resized_size(size => "2592x1944", resize => "100x200");Result:
[200, "OK (envelope generated)", "100x75"]fit image inside 100x100:
calc_image_resized_size(size => "2592x1944", resize => "100x100");Result:
[200, "OK (envelope generated)", "100x75"]fit a 10000x5000 area inside image:
calc_image_resized_size(size => "2592x1944", resize => "10000x5000^");Result:
[200, "OK (envelope generated)", "10000x7500"]fit a 5000x10000 area inside image:
calc_image_resized_size(size => "2592x1944", resize => "5000x10000^");Result:
[200, "OK (envelope generated)", "13333x10000"]fit a 100x100 area inside image (no effect since the image can already fit that area):
calc_image_resized_size(size => "2592x1944", resize => "100x100^");Result:
[200, "OK (envelope generated)", "2592x1944"]set dimension to 100x100:
calc_image_resized_size(size => "2592x1944", resize => "100x100!");Result:
[200, "OK (envelope generated)", "100x100"]shrink image to fit inside 10000x5000px (no effect since image already fits):
calc_image_resized_size(size => "2592x1944", resize => "10000x5000>");Result:
[200, "OK (envelope generated)", "2592x1944"]shrink image to fit inside 2000x1000px:
calc_image_resized_size(size => "2592x1944", resize => "2000x1000>");Result:
[200, "OK (envelope generated)", "1333x1000"]shrink image to fit inside 100x100px:
calc_image_resized_size(size => "2592x1944", resize => "100x100>");Result:
[200, "OK (envelope generated)", "100x75"]enlarge image to fit 10000x5000px inside it:
calc_image_resized_size(size => "2592x1944", resize => "10000x5000<");Result:
[200, "OK (envelope generated)", "10000x7500"]enlarge image to fit 5000x10000px inside it:
calc_image_resized_size(size => "2592x1944", resize => "5000x10000<");Result:
[200, "OK (envelope generated)", "13333x10000"]enlarge image to fit 3000x1000px inside it (no effect since image already fits):
calc_image_resized_size(size => "2592x1944", resize => "3000x1000<");Result:
[200, "OK (envelope generated)", "2592x1944"]shrink shortest side to 1024px:
calc_image_resized_size(size => "2592x1944", resize => "1024^>");Result:
[200, "OK (envelope generated)", "1365x1024"]shrink shortest side to 10240px (no effect since shortest side 1944px is already less than 10240px):
calc_image_resized_size(size => "2592x1944", resize => "10240^>");Result:
[200, "OK (envelope generated)", "2592x1944"]enlarge shortest side to 1024px (no effect since shortest side is already greater than 1024px):
calc_image_resized_size(size => "2592x1944", resize => "1024^<");Result:
[200, "OK (envelope generated)", "2592x1944"]enlarge shortest side to 10240px:
calc_image_resized_size(size => "2592x1944", resize => "10240^<");Result:
[200, "OK (envelope generated)", "13653x10240"]shrink longest side to 1024px:
calc_image_resized_size(size => "2592x1944", resize => "^1024>");Result:
[200, "OK (envelope generated)", "1024x768"]shrink longest side to 10240px (no effect since longest side 2592px is already less than 10240px):
calc_image_resized_size(size => "2592x1944", resize => "^10240>");Result:
[200, "OK (envelope generated)", "2592x1944"]enlarge longest side to 1024px (no effect since longest side 2592px is already greater than 1024px):
calc_image_resized_size(size => "2592x1944", resize => "^1024<");Result:
[200, "OK (envelope generated)", "2592x1944"]enlarge longest side to 10240px:
calc_image_resized_size(size => "2592x1944", resize => "^10240<");Result:
[200, "OK (envelope generated)", "10240x7680"]
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
resize* => str
Resize instruction, follows ImageMagick format.
Resize instruction can be given in several formats:
Syntax Meaning -------------------------- ---------------------------------------------------------------- "" No resizing. SCALE"%" Height and width both scaled by specified percentage. SCALEX"%x"SCALEY"%" Height and width individually scaled by specified percentages. (Only one % symbol needed.) WIDTH Width given, height automagically selected to preserve aspect ratio. WIDTH">" Shrink width if larger, height automagically selected to preserve aspect ratio. WIDTH"^" Enlarge width if smaller, height automagically selected to preserve aspect ratio. "x"HEIGHT Height given, width automagically selected to preserve aspect ratio. "x"HEIGHT">" Shrink height if larger, width automagically selected to preserve aspect ratio. "x"HEIGHT"^" Enlarge height if smaller, width automagically selected to preserve aspect ratio. WIDTH"x"HEIGHT Maximum values of height and width given, aspect ratio preserved. WIDTH"x"HEIGHT"^" Minimum values of height and width given, aspect ratio preserved. WIDTH"x"HEIGHT"!" Width and height emphatically given, original aspect ratio ignored. WIDTH"x"HEIGHT">" Shrinks an image with dimension(s) larger than the corresponding width and/or height argument(s). WIDTH"x"HEIGHT"<" Shrinks an image with dimension(s) larger than the corresponding width and/or height argument(s). NUMBER"^>" Shrink shortest side if larger than number, aspect ratio preserved. NUMBER"^<" Enlarge shortest side if larger than number, aspect ratio preserved. "^"NUMBER">" Shrink longer side if larger than number, aspect ratio preserved. "^"NUMBER"<" Enlarge longer side if larger than number, aspect ratio preserved.Currently unsupported:
AREA"@" Resize image to have specified area in pixels. Aspect ratio is preserved. X":"Y Here x and y denotes an aspect ratio (e.g. 3:2 = 1.5).Ref: http://www.imagemagick.org/script/command-line-processing.php#geometry
size* => str
Image/video size, in <width>x<height> format, e.g. 2592x1944.
Returns an enveloped result (an array).
First element ($status_code) is an integer containing HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing error message, or something like "OK" if status is 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional, a hash that contains extra information, much like how HTTP response headers provide additional metadata.
Return value: (any)
image_resize_notation_to_human
Usage:
image_resize_notation_to_human(%args) -> [$status_code, $reason, $payload, \%result_meta]
Translate ImageMagick-like resize notation (e.g. "720^>") to human-friendly text (e.g. "shrink shortest side to 720px").
Examples:
Example #1:
image_resize_notation_to_human(resize => ""); # -> [200, "OK (envelope generated)", "no resizing"]Example #2:
image_resize_notation_to_human(resize => "50%"); # -> [200, "OK (envelope generated)", "scale to 50%"]Example #3:
image_resize_notation_to_human(resize => "50%x50%");Result:
[ 200, "OK (envelope generated)", "scale width to 50%, height to 50%", ]Example #4:
image_resize_notation_to_human(resize => 720); # -> [200, "OK (envelope generated)", "set width to 720px"]Example #5:
image_resize_notation_to_human(resize => "720>"); # -> [200, "OK (envelope generated)", "shrink width to 720px"]Example #6:
image_resize_notation_to_human(resize => "720^"); # -> [200, "OK (envelope generated)", "enlarge width to 720px"]Example #7:
image_resize_notation_to_human(resize => "x720"); # -> [200, "OK (envelope generated)", "set height to 720px"]Example #8:
image_resize_notation_to_human(resize => "x720>"); # -> [200, "OK (envelope generated)", "shrink height to 720px"]Example #9:
image_resize_notation_to_human(resize => "x720^"); # -> [200, "OK (envelope generated)", "enlarge height to 720px"]Example #10:
image_resize_notation_to_human(resize => "640x480"); # -> [200, "OK (envelope generated)", "fit image inside 640x480"]Example #11:
image_resize_notation_to_human(resize => "640x480^");Result:
[ 200, "OK (envelope generated)", "fit image to fit 640x480 inside it", ]Example #12:
image_resize_notation_to_human(resize => "640x480>");Result:
[ 200, "OK (envelope generated)", "shrink image to fit inside 640x480", ]Example #13:
image_resize_notation_to_human(resize => "640x480<");Result:
[ 200, "OK (envelope generated)", "enlarge image to fit 640x480 inside it", ]Example #14:
image_resize_notation_to_human(resize => "640x480!");Result:
[200, "OK (envelope generated)", "set dimension to 640x480"]Example #15:
image_resize_notation_to_human(resize => "720^>");Result:
[ 200, "OK (envelope generated)", "shrink shortest side to 720px", ]Example #16:
image_resize_notation_to_human(resize => "720^<");Result:
[ 200, "OK (envelope generated)", "enlarge shortest side to 720px", ]Example #17:
image_resize_notation_to_human(resize => "^720>");Result:
[200, "OK (envelope generated)", "shrink longest side to 720px"]Example #18:
image_resize_notation_to_human(resize => "^720<");Result:
[ 200, "OK (envelope generated)", "enlarge longest side to 720px", ]
Resize notation supports most syntax from ImageMagick geometry. See Math::Image::CalcResized and ImageMagick documentation on geometry for more details.
This function is not exported.
Arguments ('*' denotes required arguments):
resize* => str
(No description)
Returns an enveloped result (an array).
First element ($status_code) is an integer containing HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing error message, or something like "OK" if status is 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional, a hash that contains extra information, much like how HTTP response headers provide additional metadata.
Return value: (any)
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Math-Image-CalcResized.
SOURCE
Source repository is at https://github.com/perlancar/perl-Math-Image-CalcResized.
SEE ALSO
AUTHOR
perlancar <perlancar@cpan.org>
CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
COPYRIGHT AND LICENSE
This software is copyright (c) 2024, 2021, 2020 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Math-Image-CalcResized
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.