resize - Perl - Image::Magick create specific size image "canvas" while maintaining aspect ratio of original image? -
i need create images within 480w x 360h pixel "canvas".
i did create images remote url, no problem stackoverflow.
however, desire maintain aspect ratio of image but, have end result 480x360.. therefore, "canvas" or border crop technique needs used (from have read) but, cannot seem going.
here have:
#!/usr/bin/perl use image::resize; use image::magick; use strict; $new = 'path/to/image/image.jpg'; $somewords = 'some words'; $imageurl='http://myimageurl.com/image.jpg'; $p = new image::magick; $p->read("$imageurl"); ($origw, $origh) = $p->get('width', 'height'); #### correct size images processed here annotation ######## if (($origw == 480) && ($origh == 360)){ system("convert $imageurl -fill '#ffffff' -font candice -pointsize 12 -undercolor '#00000080' -gravity southeast -annotate +1+1 '$somewords' $new"); } #### process images of incorrect original size stuck ####### if (($origw != 480) && ($origh != 360)){ system("convert $imageurl $new"); system("convert $imageurl -resize 480x360\! -fill '#ffffff' -font candice -pointsize 14 -undercolor '#00000080' -gravity southwest -annotate +1+1 '$somewords' $new"); }
what need this:
a "canvas" size of 480 x 360.
reduce original image url correct aspect ratio @ either 480w or 360h , place in middle of 480x360 canvas.
i read somewhere, offered no examples, resize original image while maintaining aspect ratio correct height or width whichever allows image largest then, divide other param (h or w) 2 , make add border based on that, crop size. confused "he + double hockey sticks" out of me.
i lost on trying figure out. unsure if question here clear , worthy of asking stackoverflow.
seems resizing while maintaining aspect ratio while creating fixed output image difficult! hours of searching have not helped me.
i praise 1 offers verbose solution. thanks.
isn't bit silly use image::magick
module, , use external convert
command? can of within perl script using image::magick
.
anyway, if read fine manual, you'll find imagemagick resize highest dimensions within 480x360 without changing aspect ratio using 480x360
. works both on command-line convert
, within image::magick
. when add !
, you're telling resize 480x360, disregarding aspect ratio.
this should started without using external commands:
... $p->resize(geometry=>'480x360'); $p = $p->montage(geometry=>'480x360', background=>'black', fill=>'white', stroke=>'white', pointsize=>12, title=>$somewords); $p->write($new); ...
Comments
Post a Comment