I managed to install this package via my linux package manager along with the library.

Here is something to get you started from first principles:

#!/usr/bin/perl # by bliako # for https://perlmonks.org/?node_id=11138293 # on 01/11/2021 use strict; use warnings; use Graphics::Magick; my($image, $status); $image = Graphics::Magick->new; $status = $image->Read('a.jpg'); my $W = $image->Get('width'); my $H = $image->Get('height'); my $maxW = 800; my $maxH = 200; my ($newW, $newH, $factor) = trans($W, $H, $maxW, $maxH); my $AR = $W / $H; my $newAR = $newW / $newH; print "($W x $H) => ($newW x $newH)\n"; print "original aspect ratio: $AR, new aspect ratio:$newAR\n"; $image->Resize(width => $newW, height => $newH); $image->write("resized.jpg"); #testme(); # given original dimensions and new max dimensions # it returns (newW, newH, resize_factor) sub trans { my ($W, $H, $maxW, $maxH) = @_; my ($arW, $arH); my $factor = ($arW=($W/$maxW)) < ($arH=($H/$maxH)) ? $arH : $arW; my $newW = int($W/$factor); my $newH = int($H/$factor); return ($newW, $newH, $factor); } # this does a brute-force testing on sizes 10-1000 # for 800x200 max dim sub testme { my $maxW = 800; my $maxH = 200; for my $W (map { $_*10 } 1..100){ for my $H (map { $_*10 } 1..100){ my ($newW, $newH, $factor) = trans($W, $H, $maxW, $maxH); my $AR = $W / $H; my $newAR = $newW / $newH; die "failed test: aspect ratios not the same ($AR -> $newAR) for + ($W x $H) => ($newW x $newH)" if ($AR-$newAR) > 10E-1; die "failed test: new width exceeds maximum for ($W x $H) => ($n +ewW x $newH)" if $newW > $maxW; die "failed test: new height ($newH) exceeds maximum for ($W x $ +H) => ($newW x $newH)" if $newH > $maxH; } } }

Output:

(2560 x 1536) => (333 x 200) original aspect ratio: 1.66666666666667, new aspect ratio:1.665

bw, bliako


In reply to Re: graphicsmagick perl shrink image to size by bliako
in thread graphicsmagick perl shrink image to size by melutovich

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.