人力検索はてな
モバイル版を表示しています。PC版はこちら
i-mobile

perlのimager で、背景などの透過PNGは、作成できますか?
できたら教えてください。

●質問者: Kamesuta
●カテゴリ:コンピュータ ウェブ制作
○ 状態 :終了
└ 回答数 : 1/1件

▽最新の回答へ

1 ● TransFreeBSD
ベストアンサー

http://search.cpan.org/perldoc?Imager::Cookbook#Transparent_PNG

To make a transparent image, create an image object with 2 or 4 channels:

 # RGB with alpha channel
 my $rgba = Imager->new(xsize => $width, ysize => $height, channels => 4);

 # Gray with alpha channel
 my $graya = Imager->new(xsize => $width, ysize => $height, channels => 2);

By default, the created image will be transparent.

最後の行にあれっ?と思ったが、デフォルトのチャンネルは3、つまり透過を表すアルファチャンネルがないのは確か。
http://search.cpan.org/perldoc?Imager::ImageTypes#Creating_Imager_Objects

channels - The number of channels for the image. Default 3. Valid values are from 1 to 4.

たぶん、アルファチャンネル付きだと、作成時は全域が透過状態という意味なのでしょう。


Kamesutaさんのコメント
新たにモジュールを入れないといけないですか? ただ「channels => 4」を入れただけではできませんでした。 もう少し具体的にお願いします。

TransFreeBSDさんのコメント
特に追加しなければならないモジュールはないと思います。 私の所では以下で周囲が透明なpng画像が出来ています。 >|perl| use strict; use warnings; use Imager; my $image = Imager->new(xsize => 100, ysize => 100, , channels => 4); $image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79, filled => 1, color => 'green'); $image->write(file=>'box.png') or die 'Cannot save box.png: ', $image->errstr; ||<

Kamesutaさんのコメント
#!/usr/bin/perl use Imager; my $string = "ああああ"; my $font = Imager::Font->new( file => '/home/XXXX/public_html/font/font001.TTF', color => '#000000', size => 60, ); my $bbox = $font->bounding_box(string => $string); # 文字情報を取得 my $img = Imager->new(xsize => $bbox->total_width*1.1, ysize => 70, ,channels => 4);←これを付けて、、 #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ #$img->box(filled => 1);←←これを消したらできました。 #↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ $img->string( string => $string, utf8 => 1, font => $font, x => 0, y => 60, aa => 1, ); print "Content-type: image/png\n\n"; print $img->write(fh => \*STDOUT, type => 'png') or die $img->errstr; 変なボックスを描画していたからできなかったのですね。 ありがとうございます
関連質問

●質問をもっと探す●



0.人力検索はてなトップ
8.このページを友達に紹介
9.このページの先頭へ
対応機種一覧
お問い合わせ
ヘルプ/お知らせ
ログイン
無料ユーザー登録
はてなトップ