#!/usr/local/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use lib qw(../lib2);
# use strict;
# use warnings;
use CGI;
use Image::Magick;
use Unicode::Japanese;
#------------------------------------------------------------
# 設定
#------------------------------------------------------------
# ディレクトリ指定
my $dir = './pic';
# 挿入文字列1
my $text1 = "旅の空から"; # "123☆45";
# 挿入位置
my $pos1 = ' North'; # NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast から選択
# 文字大きさ
my $p1d = 20;
# 小さい画像の文字大きさ
my $p1m = 10;
# 縮小画像の文字大きさ
my $p1s = 8;
# フォント
my $font1 = '../ipagui.ttf';
# 横位置
my $w1d = 0;
# 小さい画像の横位置
my $w1m = 0;
# 縮小画像の横位置
my $w1s = 0;
# 縦位置
my $h1d = 10;
# 小さい画像の縦位置
my $h1m = 8;
# 縮小画像の縦位置
my $h1s = 6;
# 文字色
my $color1 = '#ffffff';
# 背景色
my $bgcolor1 = '#000000';
# 挿入文字列2
my $text2 = 'http://〜〜〜/bbs/yybbs.cgi';
# 挿入位置
my $pos2 = 'SouthEast'; # NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast から選択
# 文字大きさ
my $p2d = 14;
# 小さい画像の文字大きさ
my $p2m = 4;
# 縮小画像の文字大きさ
my $p2s = 2;
# フォント
my $font2 = '../ipagui.ttf';
# 横位置
my $w2d = 10;
# 小さい画像の横位置
my $w2m = 6;
# 縮小画像の横位置
my $w2s = 4;
# 縦位置
my $h2d = 10;
# 小さい画像の縦位置
my $h2m = 6;
# 縮小画像の縦位置
my $h2s = 4;
# 文字色
my $color2 = '#ffffff';
# 背景色
my $bgcolor2 = '#000000';
#------------------------------------------------------------
# 設定 終わり
#------------------------------------------------------------
#------------------------------------------------------------
# メインルーチン
#------------------------------------------------------------
# sjis→utf8変換
$text1 = Unicode::Japanese->new($text1, 'sjis')->utf8;
$text2 = Unicode::Japanese->new($text2, 'sjis')->utf8;
# 指定ディレクトリオープン
opendir(DIR, $dir) || &error("Can't Open $dir");
# ファイル名でソート 拡張子gif jpg pngのファイルのみ
my @file = sort grep { /^(s?\d+(\-\d)?)\.(gif|jpg|png)$/i && -f "$dir/$_"} readdir(DIR);
# 大文字小文字を区別せずソート
@file = sort { lc $a cmp lc $b } @file;
closedir(DIR);
# ヘッダ出力
print "Content-type: text/html; charset=Shift_JIS\n\n";
print <<"EOM";
画像ファイル 文字挿入
EOM
# ここからループ
my $i = 0;
foreach (@file) {
# 保存された元ファイルはそのまま
if ($_ =~ /(.+)\-org\.(.+)/) { next; }
$i++;
# 元ファイル保存
$_ =~ /^(s?\d+(\-\d)?)\.(gif|jpg|png)$/;
my $fn1 = $1;
my $fn2 = $3;
# ファイル指定
my $infile;
my $outfile = "$dir/$_";
if (-e "$dir/${fn1}-org.$fn2") {
$infile = "$dir/${fn1}-org.$fn2";
} else {
$infile = "$dir/$_";
}
# 元ファイル保存
use File::Copy;
unless (-e "$dir/${fn1}-org.$fn2") {
copy($infile,"$dir/${fn1}-org.$fn2") or &error("Copy failed : $dir/${fn1}-org.$fn2");
}
print qq |$i. $_ (org)|;
# Image::Magick オブジェクト作成
my $image = Image::Magick->new;
$image->Read($infile);
# 画像情報
my ($width, $height, $format) = $image->Get('width', 'height', 'magick');
print qq | . $width×$height $format . |;
# 文字大きさ、横位置、縦位置
my ($p1,$w1,$h1);
my ($p2,$w2,$h2);
# 小さすぎる画像は挿入なし
if ($width < 50 || $height < 50) {
print qq | ... smaller Skip
\n|;
next;
# 縮小ファイル時またはすごく小さい画像の時
} elsif ($_ =~ /^s/ || $width < 150 || $height < 150) {
$p1 = $p1s; $w1 = $w1s; $h1 = $h1s;
$p2 = $p2s; $w2 = $w2s; $h2 = $h2s;
# 小さい画像の時
} elsif ($width < 300 || $height < 300) {
$p1 = $p1m; $w1 = $w1m; $h1 = $h1m;
$p2 = $p2m; $w2 = $w2m; $h2 = $h2m;
} else {
$p1 = $p1d; $w1 = $w1d; $h1 = $h1d;
$p2 = $p2d; $w2 = $w2d; $h2 = $h2d;
}
# 文字埋め込み1
if ($text1 ne '' && $bgcolor1 ne '') {
$image -> Annotate(
text => $text1,
fill => $color1,
undercolor => $bgcolor1,
gravity => $pos1,
font => $font1,
pointsize => $p1,
x => $w1,
y => $h1,
encoding => 'UTF-8',
);
} elsif ($text1 ne '') {
$image -> Annotate(
text => $text1,
fill => $color1,
gravity => $pos1,
font => $font1,
pointsize => $p1,
x => $w1,
y => $h1,
encoding => 'UTF-8',
);
}
# 文字埋め込み2
if ($text2 ne '' && $bgcolor2 ne '') {
$image -> Annotate (
text => $text2,
fill => $color2,
undercolor => $bgcolor2,
gravity => $pos2,
font => $font2,
pointsize => $p2,
x => $w2,
y => $h2,
encoding => 'UTF-8',
);
} elsif ($text2 ne '') {
$image -> Annotate (
text => $text2,
fill => $color2,
gravity => $pos2,
font => $font2,
pointsize => $p2,
x => $w2,
y => $h2,
encoding => 'UTF-8',
);
}
print qq | ...|;
# 画像出力
binmode STDOUT;
$image->Write($outfile);
print qq | OK
\n|;
# ここまでループ
}
print <<"EOM";
EOM
exit;
#------------------------------------------------------------
# エラー処理
#------------------------------------------------------------
sub error {
print "Content-type: text/html; charset=Shift_JIS\n\n";
print <<"EOM";
画像ファイル 文字挿入 -ERROR-
- E R R O R -
$_[0]
EOM
exit;
}
# Annotate text=>string, font=>string, family=>string, style=>{Normal, Italic, Oblique, Any}, stretch=>{Normal, UltraCondensed, ExtraCondensed, Condensed, SemiCondensed, SemiExpanded, Expanded, ExtraExpanded, UltraExpanded}, weight=>integer, pointsize=>integer, density=>geometry, stroke=>color name, strokewidth=>integer, fill=>color name, undercolor=>color name, geometry=>geometry, gravity=>{NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast}, antialias=>{true, false}, x=>integer, y=>integer, affine=>array of float values, translate=>float, float, scale=>float, float, rotate=>float. skewX=>float, skewY=> float, align=>{Left, Center, Right}, encoding=>{UTF-8}
# http://www.ss.iij4u.or.jp/~somali/web/_perlmagick_ref.html
# http://treceed.s3.xrea.com/x/blog/blog.cgi?n=7
# http://blog.dtpwiki.jp/dtp/2006/07/imagemagickperl_6b50.html