sub thumbnail{ # receives: # file source (absolute path on machine) # file target out (absolute path on machine) # restriction # restriction type # quality my ($src,$out,$restriction,$restrict_type,$quality)=@_; # both restrict to height and width :both won't go ov +er this.. # width restrict to width # height restrict to height # square square - will crop sides or top and bottom to +fit. neat. my $img = Image::Magick->new; #blank $img->Read($src); #read source file into blank unless($img){return 0;} my ($w,$h) = $img->Get('width','height'); # find dimensions unless($w and $h){return 0;} # # # #if width is bigger then height, we use restrict to w if ($restrict_type eq 'both'){ if ($w>$h){ $restrict_type='width'; } else { $restrict_type='height'; } } my $newh; my $neww; # initialize new dimensions # not used if +'square' if ($restrict_type eq 'height') { $neww=(($w*$restriction)/$h); $img->Resize(width=>$neww, height=>$restriction); } if ($restrict_type eq 'width') { $newh=(($h*$restriction)/$w); $img->Resize(width=>$restriction, height=>$newh); } if ($restrict_type eq 'square'){ # my ($cut,$xcut,$ycut); if ($w>$h){ $cut=$h; $xcut=(($w-$h)/2); $ycut=0; } if ($w<$h){ $cut=$w; $xcut=0; $ycut=(($h-$w)/2); } $img->Crop(width=>$cut,height=>$cut,x=>$xcut,y=>$ycut) +; $img->Resize(width=>$restriction, height=>$restriction +); # } $img->Set(quality=>$quality); $img->Write($out); undef $img; return 1; }

Usage: for a square thumb 80px each side:

$image_in=$ENV{DOCUMENT_ROOT}.'/img/1.jpg'; #example, might have ENV D +OC ROOT on your server, need full pathof some sort though $image_out=$ENV{DOCUMENT_ROOT}.'/img/.thumb.1.jpg'; #whatever you want + to call it. thumbnail($image_in, $image_out,80,'square',90);

update: I made a module to make a square image as we talk abot here, Image::Magick::Square . And another to automatically make thumbnails server side , with square option, Image::Magick::Thumbnail::NotFound


In reply to Re: Make a Square Thumbnail by leocharre
in thread Make a Square Thumbnail by debiandude

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.