メメメモモ

プログラミング、筋トレ、ゲーム、etc

GD::SecurityImageを使ってCAPTCHA画像を生成

GD::SecurityImageを使ってCAPTCHA画像を生成を試してみました。
GDが入っていなかったので、インストール手順から。

GDのインストール

CPANモジュールを入れる前に下記のリンクからライブラリをDLしてきてインストールします。
http://www.libgd.org/Downloads

$ wget http://www.libgd.org/releases/gd-2.0.35.tar.gz
$ tar xvzf gd-2.0.35.tar.gz
$ cd gd-2.0.35
$ ./configure
$ make
$ sudo make install


ライブラリをインストールできたらcpanでモジュールをインストールします。

$ sudo cpan GD::SecurityImage

GD::SecurityImageのパラメータ確認

use strict;
use warnings;

use GD::SecurityImage;

my %create_p = ();
$create_p{style} = 'circle';
$create_p{fontcol} = '#0066cc';
$create_p{linecol} = '#0066cc';

my %new_p = ();
$new_p{lines} = 20;
$new_p{angle} = 0;
$new_p{scramble} = 1;
$new_p{ptsize} = 30;
$new_p{ptrange} = 0;
$new_p{thickness} = 1;
$new_p{bgcolor} = '#eeeeee';

my %part_p = ();
$part_p{particle1} = 100;
$part_p{particle2} = 1;

my $config = {
    new => {
        width => 300,
        height => 100,
        rnd_data => ['a'..'z','A'..'Z',1..9],
        font => 'ttfファイルへの絶対パス',
        %new_p,
    },
    create => [ 'ttf', $create_p{style}, $create_p{fontcol}, $create_p{linecol} ],
    particle => [ $part_p{particle1}, $part_p{particle2} ],
};

my $image = GD::SecurityImage->new( %{$config->{new}} );
$image->random();
$image->create( @{$config->{create}} );
$image->particle( @{$config->{particle}} );
my ($image_data, $mime_type, $random_number) = $image->out( force => 'jpeg' );

print "$mime_type, $random_number\n";
open my $fh, '>', "$random_number.jpg";
binmode($fh);
print $fh $image_data;
close($fh);


$create_p{style} = 'circle';
f:id:memememomo:20100501103844j:image



$create_p{style} = 'rect';
f:id:memememomo:20100501103907j:image



$create_p{style} = 'ec';
f:id:memememomo:20100501103802j:image