The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Math::Image::CalcResized - Calculate dimensions of image/video resized by ImageMagick-like geometry specification

VERSION

This document describes version 0.005 of Math::Image::CalcResized (from Perl distribution Math-Image-CalcResized), released on 2021-10-12.

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:

  • Example #1:

     calc_image_resized_size(size => "2592x1944", resize => ""); # -> [200, "OK (envelope generated)", "2592x1944"]
  • Example #2:

     calc_image_resized_size(size => "2592x1944", resize => "20%"); # -> [200, "OK (envelope generated)", "518x388"]
  • Example #3:

     calc_image_resized_size(size => "2592x1944", resize => "20%x40%");

    Result:

     [200, "OK (envelope generated)", "518x777"]
  • Example #4:

     calc_image_resized_size(size => "2592x1944", resize => "20x40%");

    Result:

     [200, "OK (envelope generated)", "518x777"]
  • Example #5:

     calc_image_resized_size(size => "2592x1944", resize => 1024); # -> [200, "OK (envelope generated)", "1024x768"]
  • Example #6:

     calc_image_resized_size(size => "2592x1944", resize => "1024>");

    Result:

     [200, "OK (envelope generated)", "1024x768"]
  • Example #7:

     calc_image_resized_size(size => "2592x1944", resize => "10240>");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #8:

     calc_image_resized_size(size => "2592x1944", resize => "1024^");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #9:

     calc_image_resized_size(size => "2592x1944", resize => "10240^");

    Result:

     [200, "OK (envelope generated)", "10240x7680"]
  • Example #10:

     calc_image_resized_size(size => "2592x1944", resize => "x1024");

    Result:

     [200, "OK (envelope generated)", "1365x1024"]
  • Example #11:

     calc_image_resized_size(size => "2592x1944", resize => "x768>");

    Result:

     [200, "OK (envelope generated)", "1024x768"]
  • Example #12:

     calc_image_resized_size(size => "2592x1944", resize => "x7680>");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #13:

     calc_image_resized_size(size => "2592x1944", resize => "x768^");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #14:

     calc_image_resized_size(size => "2592x1944", resize => "x7680^");

    Result:

     [200, "OK (envelope generated)", "10240x7680"]
  • Example #15:

     calc_image_resized_size(size => "2592x1944", resize => "20000x10000");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #16:

     calc_image_resized_size(size => "2592x1944", resize => "20000x1000");

    Result:

     [200, "OK (envelope generated)", "1333x1000"]
  • Example #17:

     calc_image_resized_size(size => "2592x1944", resize => "100x200");

    Result:

     [200, "OK (envelope generated)", "100x75"]
  • Example #18:

     calc_image_resized_size(size => "2592x1944", resize => "100x100");

    Result:

     [200, "OK (envelope generated)", "100x75"]
  • Example #19:

     calc_image_resized_size(size => "2592x1944", resize => "10000x5000^");

    Result:

     [200, "OK (envelope generated)", "10000x7500"]
  • Example #20:

     calc_image_resized_size(size => "2592x1944", resize => "5000x10000^");

    Result:

     [200, "OK (envelope generated)", "13333x10000"]
  • Example #21:

     calc_image_resized_size(size => "2592x1944", resize => "100x100^");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #22:

     calc_image_resized_size(size => "2592x1944", resize => "100x100!");

    Result:

     [200, "OK (envelope generated)", "100x100"]
  • Example #23:

     calc_image_resized_size(size => "2592x1944", resize => "10000x5000>");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #24:

     calc_image_resized_size(size => "2592x1944", resize => "5000x10000>");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #25:

     calc_image_resized_size(size => "2592x1944", resize => "3000x1000>");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #26:

     calc_image_resized_size(size => "2592x1944", resize => "2000x1000>");

    Result:

     [200, "OK (envelope generated)", "1333x1000"]
  • Example #27:

     calc_image_resized_size(size => "2592x1944", resize => "100x100>");

    Result:

     [200, "OK (envelope generated)", "100x75"]
  • Example #28:

     calc_image_resized_size(size => "2592x1944", resize => "10000x5000<");

    Result:

     [200, "OK (envelope generated)", "10000x7500"]
  • Example #29:

     calc_image_resized_size(size => "2592x1944", resize => "5000x10000<");

    Result:

     [200, "OK (envelope generated)", "13333x10000"]
  • Example #30:

     calc_image_resized_size(size => "2592x1944", resize => "3000x1000<");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #31:

     calc_image_resized_size(size => "2592x1944", resize => "2000x1000<");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #32:

     calc_image_resized_size(size => "2592x1944", resize => "100x100<");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #33:

     calc_image_resized_size(size => "2592x1944", resize => "1024^>");

    Result:

     [200, "OK (envelope generated)", "1365x1024"]
  • Example #34:

     calc_image_resized_size(size => "2592x1944", resize => "10240^>");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #35:

     calc_image_resized_size(size => "2592x1944", resize => "1024^<");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #36:

     calc_image_resized_size(size => "2592x1944", resize => "10240^<");

    Result:

     [200, "OK (envelope generated)", "13653x10240"]
  • Example #37:

     calc_image_resized_size(size => "2592x1944", resize => "^1024>");

    Result:

     [200, "OK (envelope generated)", "1024x768"]
  • Example #38:

     calc_image_resized_size(size => "2592x1944", resize => "^10240>");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #39:

     calc_image_resized_size(size => "2592x1944", resize => "^1024<");

    Result:

     [200, "OK (envelope generated)", "2592x1944"]
  • Example #40:

     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

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, and sometimes one or two other Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE

This software is copyright (c) 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.