The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

GD::SecurityImage - Security image (captcha) generator.

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",
                                      scramble => 1);
      $image->random($your_random_str);
      $image->create(ttf => 'default');
      $image->particle;
   my($image_data, $mime_type, $random_number) = $image->out;

or you can just say (all public methods can be chained)

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

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

If you require the module, you must import it also:

   require GD::SecurityImage;
   import GD::SecurityImage;

or:

   require GD::SecurityImage;
   GD::SecurityImage->import;

if you don't like indirect object syntax.

If you dont import, the required modules will not be loaded and probably, you'll die().

Beginning with v1.2, the module supports Image::Magick, but the default interface uses GD module. To enable Image::Magick support, you must call the module with the use_magick option:

   use GD::SecurityImage use_magick => 1;

If you require the module, you must import it also:

   require GD::SecurityImage;
   import GD::SecurityImage use_magick => 1;

or:

   require GD::SecurityImage;
   GD::SecurityImage->import(use_magick => 1);

if you don't like indirect object syntax.

If you dont import, the required modules will not be loaded and probably, you'll die().

The module does not export anything actually. But import loads the necessary sub modules.

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). Security images are basicly, graphical CAPTCHAs (Completely Automated Public Turing Test to Tell Computers and Humans Apart). 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. Only necessarry if you want to use a ttf font 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.

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. Passed as an arrayref with three elements (red, green, blue).

send_ctobg

If has a true value, the random security code will be displayed in the background and the lines will pass over it. (send_ctobg = send code to background)

frame

If has a true value, a frame will be added around the image. This option is enabled by default.

scramble

If set, the characters will be scrambled. If you enable this option, be sure to use a wider image, since the characters will be separeted with three spaces.

angle

Sets the angle for scrambled characters. Beware that, if you pass an angle parameter, the characters in your random string will have a fixed angle. If you do not set an angle parameter, the angle(s) will be random.

Unlike the GD interface, $angle is in degrees and can take values between 0 and 360.

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.

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 (defined by an object table) 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 one of the following (some of the styles may not work if you are using a really old version of GD):

default

The default style. Draws horizontal, vertical and angular lines.

rect

Draws horizontal and vertical lines

box

Draws two filled rectangles.

The lines option passed to new, controls the size of the inner rectangle for this style. If you increase the lines, you'll get a smaller internal rectangle. Using smaller values like 5 can be better.

circle

Draws circles.

ellipse

Draws ellipses.

ec

This is the combination of ellipse and circle styles. Draws both ellipses and circles.

Note: if you have a (very) old version of GD, you may not be able to use some of the styles.

The last two arguments ($text_color and $line_color) are the colors used in the image (text and line color -- respectively) and they are passed as a 3-element (red, green and blue) arrayref.

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

particle

Must be called after create.

Adds random dots to the image. They'll cover all over the surface. Accepts two parameters; the density (number) of the particles and the maximum number of dots around the main dot.

   $image->particle($density, $maxdots);

Default value of $density is dependent on your image' s width or height value. The greater value of width and height is taken and multiplied by twenty. So; if your width is 200 and height is 70, $density is 200 * 20 = 4000 (unless you pass your own value). The default value of $density can be too much for smaller images.

$maxdots defines the maximum number of dots near the default dot. Default value is 1. If you set it to 4, The selected pixel and 3 other pixels near it will be used and colored.

The color of the particles are the same as the color of your text (defined in create).

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 type, while new versions support jpeg and png (update: beginning with v2.15, GD resumed gif support).

The returned mime type is either gif or jpeg for GD and gif for Image::Magick (if you do not force some other format).

out method accepts arguments:

   @data = $image->out(%args);

currently, you can only set output format with the force key:

   @data = $image->out(force => 'png');

If png is supported by the interface (via GD or Image::Magick); you'll get a png image, if the interface does not support this format, out() method will use it's default configuration.

Currently, you can not define compression values for the formats that support it (eg: jpeg, png), but you can use raw method instead of out (for a direct communication with the graphic library -- but probably you do not want to do that, future versions may implement this feature).

raw

Depending on your usage of the module; returns the raw GD::Image object:

   my $i = $image->raw;
   print $i->png;

or the raw Image::Magick object:

   my $i = $image->raw;
   $i->Write("gif:-");

Can be usefull, if you want to modify the graphic yourself, or want to use another output format like png.

EXAMPLES

See the tests in the distribution.

ERROR HANDLING

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

SEE ALSO

GD, Image::Magick, ImagePwd, Authen::Captcha,

ImageCode Perl Module (commercial): http://www.progland.com/ImageCode.html.

The CAPTCHA project: http://www.captcha.net/.

CAVEAT EMPTOR

  • Using the default library GD is a better choice. Since it is faster and does not use that much memory, while Image::Magick is slower and uses more memory.

  • The internal random code generator is used only for demonstration purposes for this module. It may not be effective. You must supply your own random code and use this module to display it.

BUGS

Image::Magick bug

There is a bug in PerlMagick' s QueryFontMetrics() method. ImageMagick versions smaller than 6.0.4 is affected. Below text is from the ImageMagick 6.0.4 Changelog: http://www.imagemagick.org/www/Changelog.html.

"2004-05-06 PerlMagick's QueryFontMetrics() incorrectly reports `unrecognized attribute'` for the `font' attribute."

Please upgrade to ImageMagick 6.0.4 or any newer version, if your ImageMagick version is smaller than 6.0.4 and you want to use Image::Magick as the backend for GD::SecurityImage.

GD bug

libgd and GD.pm does not like relative paths and paths that have spaces in it. If you pass a font path that is not an exact path or a path that have a space in it, you may get an empty image.

To check if the module failed to find the ttf font (when using GD), a new method added: gdbox_empty(). It must be called after create():

   $image->create;
   die "Error loading ttf font for GD: $@" if $image->gdbox_empty;

gdbox_empty() always returns false, if you are using Image::Magick.

Note: New versions of GD does not have this bug. You can upgrade to v2.16 or any newer version to fix this.

Contact the author if you find any bugs. You can also send requests.

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.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 575:

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