Skip to content

Instantly share code, notes, and snippets.

@parserpro
Last active May 5, 2017 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parserpro/4b677683bbb0ec7a3391cbba390cf6a7 to your computer and use it in GitHub Desktop.
Save parserpro/4b677683bbb0ec7a3391cbba390cf6a7 to your computer and use it in GitHub Desktop.
use GD;
sub resize_img {
my ( $img, $x, $y ) = @_;
my $jpeg_quality = 60; # Качество картинки на выходе
my $max_sizex;
my $max_sizey;
GD::Image->trueColor(1); # без этого будет жопа
my $bildle = GD::Image->new( $img );
my ( $width, $height ) = $bildle->getBounds;
$max_sizex = $width / $x;
$max_sizey = $height / $y;
if ( $max_sizex > $max_sizey ) {
$x = $width / $max_sizex;
$y = $height / $max_sizex;
} else {
$x = $width / $max_sizey;
$y = $height / $max_sizey;
}
#create new picture (thumbnail)
my $bild=GD::Image->new( $x, $y, 1 );
$bild->copyResampled($bildle,0,0,0,0,$x,$y,$width,$height);
$bild->interlaced('true'); # хзчт, надо смотреть доку
return $bild->jpeg($jpeg_quality);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment