NAME

GD::SecurityImage - Create a security image with a random string on it.

SYNOPSIS

use GD::SecurityImage;

# Create a normal image
my $image = GD::SecurityImage->new(width   => 80,
                                   height  => 30,
                                   lines   => 10,
                                   gd_font => 'giant');
   $image->random($your_random_str);
   $image->create(normal => 'rect');
my($image_data, $mime_type, $random_number) = $image->out;

# use external ttf font
my $image = GD::SecurityImage->new(width  => 100,
                                   height => 40,
                                   lines  => 10,
                                   font   => "/absolute/path/to/your.ttf");
   $image->random($your_random_str);
   $image->create(ttf => 'default');
my($image_data, $mime_type, $random_number) = $image->out;

or you can just say

my($image, $type, $rnd) = GD::SecurityImage->new->random->create->out;

to create a security image with the default settings. But that may not be usefull.

DESCRIPTION

The (so called) "Security Images" are so popular. Most internet software use these in their registration screens to block robot programs (which may register tons of fake member accounts). This module gives you a basic interface to create such an image. The final output is the actual graphic data, the mime type of the graphic and the created random string.

The module also has some "styles" that are used to create the background of the image.

METHODS

new

new() method takes several arguments. These arguments are listed below.

width

The width of the image (in pixels).

height

The height of the image (in pixels).

ptsize

Numerical value. The point size of the ttf character. Not necessarry unless you want to use ttf fonts in the image.

lines

The number of lines that you' ll see in the background of the image. The alignment of lines can be vertical, horizontal or angled or all of them. If you increase this parameter' s value, the image will be more cryptic.

rndmax

The length of the random string. Default value is 6.

Not necessary and will not be used if you pass your own random string.

rnd_data

Default character set used to create the random string is 0..9. But, if you want to use letters also, you can set this paramater. This paramater takes an array reference as the value.

Not necessary and will not be used if you pass your own random string.

font

The absolute path to your TrueType (.ttf) font file. Be aware that relative font paths are not recognized due to problems in the libgd library.

If you are sure that you've set this parameter to a correct value and you get warnings or you get an empty image, be sure that your path does not include spaces in it. It looks like libgd also have problems with this kind of paths (eg: '/Documents and Settings/user' under Windows).

Set this parameter if you want to use ttf in your image.

gd_font

If you want to use the default interface, set this paramater. The recognized values are Small, Large, MediumBold, Tiny, Giant. The names are case-insensitive; you can pass lower-cased parameters.

bgcolor

The background color of the image.

random

Creates the random security string or sets the random string to the value you have passed. If you pass your own random string, be aware that it must be at least six characters long.

random_str

Returns the random string. Must be called after random().

create

This method creates the actual image. It takes four arguments, but none are mandatory.

$image->create($method, $style, $text_color, $line_color);

$method can be normal or ttf. $style can be default or rect or box. The last two arguments are the colors used in the image and they are passed as a 3-element (red, green and blue) arrayref.

$image->create($method, $style, [0,0,0], [200,200,200]);

out

This method finally returns the created image, the mime type of the image and the random number generated. Older versions of GD only supports gif types, while new versions support jpeg and png.

The returned mime type is either gif or jpeg.

SEE ALSO

GD.

EXAMPLES

TTF example

#!/usr/bin/perl -w
use strict;
use CGI;
use GD::SecurityImage;

my $cgi = CGI->new;

my $ttf = "/absolute/path/to/your.ttf";

my $image = GD::SecurityImage->new(
               width    => 90,
               height   => 35,
               ptsize   => 15,
               lines    => 10,
               rndmax   => 6,
               rnd_data => [0..9, 'A'..'Z'],
               font     => $ttf,
               bgcolor  => [115, 255, 255],
);

$image->random;
$image->create(ttf => 'rect', [10,10,10], [210,210,50]);

my($image_data, $mime_type, $random_string) = $image->out;

print $cgi->header(-type => "image/$mime_type");
print $image_data;

Normal example

#!/usr/bin/perl -w
use strict;
use CGI;
use GD::SecurityImage;

my $cgi = CGI->new;

my $image = GD::SecurityImage->new(
               width    => 90,
               height   => 35,
               ptsize   => 15,
               lines    => 10,
               rndmax   => 6,
               rnd_data => [0..9, 'A'..'Z'],
               gd_font  => 'giant',
               bgcolor  => [115, 255, 255],
);

$image->random('12GH88');
$image->create(normal => 'rect', [10,10,10], [210,210,50]);

my($image_data, $mime_type, $random_string) = $image->out;

print $cgi->header(-type => "image/$mime_type");
print $image_data;

ERROR HANDLING

Currently, the module does not check the return values of GD's methods. So, if an error occurs, you can just get an empty image instead of die()ing.

BUGS

Contact the author if you find any.

AUTHOR

Burak Gürsoy, <burak@cpan.org>

COPYRIGHT

Copyright 2004 Burak Gürsoy. All rights reserved.

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

3 POD Errors

The following errors were encountered while parsing the POD:

Around line 280:

'=item' outside of any '=over'

Around line 284:

You forgot a '=back' before '=head2'

Around line 382:

Non-ASCII character seen before =encoding in 'Gürsoy,'. Assuming CP1252