use strict; use List::Util qw( max ); use Imager; use Imager::Font::Wrap; use utf8; my $photo_file = "360621461_117a18148c.jpg"; my $message = "ここは あおやま。 とうきょうの つぼ\nだという うわさのある こうえん。"; # キャンバスとなるオブジェクト my $canvas = new Imager( xsize => 450, ysize => 370 ); # 写真の読み込み my $photo = new Imager; $photo->read(file => $photo_file) or die $photo->errstr; # Maxでリサイズ my $rate = max( $canvas->getwidth / $photo->getwidth, $canvas->getheight / $photo->getheight, ); $photo = $photo->scale( xpixels => $photo->getwidth * $rate, ypixels => $photo->getheight * $rate, type => 'min', ); # キャンバスに写真を貼り付け $canvas->paste( src => $photo, left => ($canvas->getwidth - $photo->getwidth ) / 2, top => ($canvas->getheight - $photo->getheight) / 2, ); # テキストの背景エリアの画像を、、 my $msgbox = new Imager; $msgbox->read(file => 'msgbox.gif'); # キャンバスに貼り付け $canvas->paste( src => $msgbox, left => 61, top => 285, ); # フォントの読み込み my $font = Imager::Font->new(file => 'DragonQuestFC.ttf'); # 全角スペース問題 $message =~ s/\x{3000}/_/g; # adhoc # テキストをラップさせながら貼り付け Imager::Font::Wrap->wrap_text( image => $canvas, font => $font, string => $message, color => '#ffffff', size => 32, width => 315, y => 284, x => 74, ); # PNGで書き出し $canvas->write(file => "$photo_file.png");