Skip to content

Instantly share code, notes, and snippets.

@vkgtaro
Created October 2, 2009 19:02
Show Gist options
  • Save vkgtaro/200006 to your computer and use it in GitHub Desktop.
Save vkgtaro/200006 to your computer and use it in GitHub Desktop.
sub resize {
my ( $self, $key, $info ) = @_;
my ( $x, $y ) = ( $info->{x}, $info->{y} );
my $img = Imager->new();
unless ( $img->read( file => $self->request->uploads->{$key}->tempname ) ) {
$self->log->fatal($img->errstr);
return;
}
$img = $img->scale(xpixels => $x, ypixels => $y, type => 'min');
my $base_img = Imager->new( xsize => $x, ysize => $y );
# 背景画像を白で塗りつぶす。
$base_img->box(
# white
color => Imager::Color->new(255, 255, 255),
xmin => 0,
ymin => 0,
xmax => $x,
ymax => $y,
filled => 1000
);
# 変更された縦横を求める。
my $img_x = $img->getwidth;
my $img_y = $img->getheight;
# 背景画像に貼り付ける。
my $left = int(($x - $img_x) / 2);
my $top = int(($y - $img_y) / 2);
$base_img->paste( left => $left, top => $top, src => $img );
return $base_img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment