The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

VERSION

This document describes version 0.002 of Math::Image::CalcResized (from Perl distribution Math-Image-CalcResized), released on 2020-09-23.

FUNCTIONS

calc_image_resized_size

Usage:

 calc_image_resized_size(%args) -> [status, msg, payload, 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 => "20%"); # -> "518x388"
  • Example #2:

     calc_image_resized_size(size => "2592x1944", resize => "20%x40%"); # -> "518x777"
  • Example #3:

     calc_image_resized_size(size => "2592x1944", resize => "20x40%"); # -> "518x777"
  • Example #4:

     calc_image_resized_size(size => "2592x1944", resize => 1024); # -> "1024x768"
  • Example #5:

     calc_image_resized_size(size => "2592x1944", resize => "1024>"); # -> "1024x768"
  • Example #6:

     calc_image_resized_size(size => "2592x1944", resize => "10240>"); # -> "2592x1944"
  • Example #7:

     calc_image_resized_size(size => "2592x1944", resize => "1024^"); # -> "2592x1944"
  • Example #8:

     calc_image_resized_size(size => "2592x1944", resize => "10240^"); # -> "10240x7680"
  • Example #9:

     calc_image_resized_size(size => "2592x1944", resize => "x1024"); # -> "1365x1024"
  • Example #10:

     calc_image_resized_size(size => "2592x1944", resize => "x768>"); # -> "1024x768"
  • Example #11:

     calc_image_resized_size(size => "2592x1944", resize => "x7680>"); # -> "2592x1944"
  • Example #12:

     calc_image_resized_size(size => "2592x1944", resize => "x768^"); # -> "2592x1944"
  • Example #13:

     calc_image_resized_size(size => "2592x1944", resize => "x7680^"); # -> "10240x7680"
  • Example #14:

     calc_image_resized_size(size => "2592x1944", resize => "20000x10000"); # -> "2592x1944"
  • Example #15:

     calc_image_resized_size(size => "2592x1944", resize => "20000x1000"); # -> "1333x1000"
  • Example #16:

     calc_image_resized_size(size => "2592x1944", resize => "100x200"); # -> "100x75"
  • Example #17:

     calc_image_resized_size(size => "2592x1944", resize => "100x100"); # -> "100x75"
  • Example #18:

     calc_image_resized_size(size => "2592x1944", resize => "10000x5000^"); # -> "10000x7500"
  • Example #19:

     calc_image_resized_size(size => "2592x1944", resize => "5000x10000^"); # -> "13333x10000"
  • Example #20:

     calc_image_resized_size(size => "2592x1944", resize => "100x100^"); # -> "2592x1944"
  • Example #21:

     calc_image_resized_size(size => "2592x1944", resize => "100x100!"); # -> "100x100"
  • Example #22:

     calc_image_resized_size(size => "2592x1944", resize => "10000x5000>"); # -> "2592x1944"
  • Example #23:

     calc_image_resized_size(size => "2592x1944", resize => "5000x10000>"); # -> "2592x1944"
  • Example #24:

     calc_image_resized_size(size => "2592x1944", resize => "3000x1000>"); # -> "2592x1944"
  • Example #25:

     calc_image_resized_size(size => "2592x1944", resize => "2000x1000>"); # -> "1333x1000"
  • Example #26:

     calc_image_resized_size(size => "2592x1944", resize => "100x100>"); # -> "100x75"
  • Example #27:

     calc_image_resized_size(size => "2592x1944", resize => "10000x5000<"); # -> "10000x7500"
  • Example #28:

     calc_image_resized_size(size => "2592x1944", resize => "5000x10000<"); # -> "13333x10000"
  • Example #29:

     calc_image_resized_size(size => "2592x1944", resize => "3000x1000<"); # -> "2592x1944"
  • Example #30:

     calc_image_resized_size(size => "2592x1944", resize => "2000x1000<"); # -> "2592x1944"
  • Example #31:

     calc_image_resized_size(size => "2592x1944", resize => "100x100<"); # -> "2592x1944"
  • Example #32:

     calc_image_resized_size(size => "2592x1944", resize => "1024^>"); # -> "1365x1024"
  • Example #33:

     calc_image_resized_size(size => "2592x1944", resize => "10240^>"); # -> "2592x1944"
  • Example #34:

     calc_image_resized_size(size => "2592x1944", resize => "1024^<"); # -> "2592x1944"
  • Example #35:

     calc_image_resized_size(size => "2592x1944", resize => "10240^<"); # -> "13653x10240"
  • Example #36:

     calc_image_resized_size(size => "2592x1944", resize => "^1024>"); # -> "1024x768"
  • Example #37:

     calc_image_resized_size(size => "2592x1944", resize => "^10240>"); # -> "2592x1944"
  • Example #38:

     calc_image_resized_size(size => "2592x1944", resize => "^1024<"); # -> "2592x1944"
  • Example #39:

     calc_image_resized_size(size => "2592x1944", resize => "^10240<"); # -> "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
     -------------------------- ----------------------------------------------------------------
     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) is an integer containing HTTP status code (200 means OK, 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or 'OK' if status is 200. Third element (payload) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information.

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.

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.

SEE ALSO

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

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