Created
February 20, 2014 10:33
-
-
Save syachi/9110874 to your computer and use it in GitHub Desktop.
動物はあなたのごはんじゃない。の画像ジェネレータ(Hokkaido.pm Casual #22 資料)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use GD; | |
use GD::Text::Align; | |
my $jp_str = $ARGV[0] || '動物はあなたのごはんじゃない。'; | |
my $en_str = $ARGV[1] || 'Animals are not your food prodcts.'; | |
my $pic_str = $ARGV[2] || 'abcd'; | |
my $width = 600; # 横サイズ | |
my $height = 350; # 縦サイズ | |
my $margin = 1; # 枠線のマージン | |
my $gd = GD::Image->new($width, $height) or die; | |
my $bg = $gd->colorAllocate(255,255,255); # 背景色 | |
my $fg = $gd->colorAllocate(233,112,83); # 前背色 | |
# 画像サイズいっぱいに塗りつぶす | |
$gd->filledRectangle(0, 0, $width, $height, $fg); | |
# マージンの幅をとって内側を塗りつぶす | |
$gd->filledRectangle( | |
$margin, $margin, | |
($width - 1) - $margin, ($height - 1) - $margin, | |
$bg | |
); | |
# 使用フォント(本家は小塚ゴシック) | |
# http://mplus-fonts.sourceforge.jp/ | |
# http://www.dafont.com/zoologic.font | |
# 見出しを配置する | |
my $jp = GD::Text::Align->new($gd, | |
valign => 'center', | |
halign => 'center', | |
); | |
$jp->set(color => $fg); | |
$jp->set_font('mplus-1p-light.ttf', 25); | |
$jp->set_text($jp_str); | |
$jp->draw(int($width / 2), 105); | |
# 小見出しを配置する | |
my $en = GD::Text::Align->new($gd, | |
valign => 'center', | |
halign => 'center', | |
); | |
$en->set(color => $fg); | |
$en->set_font('mplus-1mn-light.ttf', 14); | |
$en->set_text($en_str); | |
$en->draw(int($width / 2), 135); | |
# 絵文字を配置する | |
my $pic = GD::Text::Align->new($gd, | |
valign => 'bottom', | |
halign => 'center', | |
); | |
$pic->set(color => $fg); | |
$pic->set_font('zoologic.ttf', 70); | |
$pic->set_text($pic_str); | |
$pic->draw(int($width / 2), 270); | |
# 画像を出力する | |
open(IMG,">image.png"); | |
binmode(IMG); | |
print IMG $gd->png(9); | |
close(IMG); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment