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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |