NAME

Mojolicious::Plugin::CaptchaPNG - PNG captcha generation and validation Mojolicious plugin

VERSION

version 1.03

SYNOPSIS

# Simple Mojolicious
$app->plugin( CaptchaPNG => { ttf => 'font.ttf' } );

my $captcha_value = $app->get_captcha_value;
my $success = $app->check_captcha_value($captcha_value);
$app->clear_captcha_value;

# Customized Mojolicious
use Math::Random::Secure 'rand';
$app->plugin( CaptchaPNG => {
    routes      => $app->routes,
    method      => 'any',
    path        => '/captcha',
    key         => '_plugin_captchapng',
    width       => 230,
    height      => 50,
    ttf         => 'font.ttf',
    size        => 20,
    rotation    => 8,
    x           => 20,
    y_base      => 35,
    y_rotate    => 100,
    noise       => 1_250,
    background  => [ 255, 255, 255 ],
    text_color  => [ 'rand(128)', 'rand(128)', 'rand(128)' ],
    noise_color => [ 'rand(128) + 128', 'rand(128) + 128', 'rand(128) + 128' ],
    value       => sub { int( rand( 10_000_000 - 1_000_000 ) ) + 1_000_000 },
    display     => sub {
        my ($display) = @_;
        $display =~ s/^(\d{2})(\d{3})/$1-$2-/;
        $display =~ s/(.)/ $1/g;
        return $display;
    },
} );

# Mojolicious::Lite
plugin( CaptchaPNG => { ttf => 'font.ttf' } );

DESCRIPTION

This module is a Mojolicious plugin for basic image captcha generation and validation.

During registration (when plugin is called), the plugin will setup a route (which defaults to /captcha) that will respond with a generated PNG captcha. The image is generated using GD::Image. The plugin will also setup helper methods to get, check, and clear the captcha value, which is stored in the session.

SETTINGS

Much of the plugin's settings are customizable, but only 1 is required.

ttf

This is the only setting that's required. It is the path to a TTF font file that will be used to generate the text in the captcha image.

routes

This a Mojolicious::Routes object. If not set, it defaults to:

$app->routes

method

The value to use when setting up the route method. If not set, it defaults to any.

path

The value to use when setting up the route path. If not set, it defaults to /captcha.

key

When a captcha image is generated, the value of the captcha text is stored in the session under this key. If not set, it defaults to _plugin_captchapng.

width, height

The width and height of the generated captcha image. If not set, these default to 230 and 50 respectively.

size

The font size of the text in the captcha image. If not set, it defaults to 20.

rotation

The amount of rotation to be made to the text in the captcha image. If not set, it defaults to 8.

x

The x coordinate GD::Image uses for the text. If not set, it defaults to 20.

y_base

The base value used for y in GD::Image for the text. If not set, it defaults to 35.

y_rotate

The rotational value used for y in GD::Image for the text. If not set, it defaults to 100.

noise

The amount of noise to generate in the image. If not set, it defaults to 1_250.

background

An array reference of 3 expressions, which will be used to set the color of the background in the image. Values will be evaluated before used. If not set, it defaults to:

[ 255, 255, 255 ]

text_color

An array reference of 3 expressions, which will be used to set the color of the text in the image. Values will be evaluated before used. If not set, it defaults to:

[ 'rand(128)', 'rand(128)', 'rand(128)' ]

noise_color

An array reference of 3 valexpressionses, which will be used to set the color of the noise color in the image. Values will be evaluated before used. If not set, it defaults to:

[ 'rand(128) + 128', 'rand(128) + 128', 'rand(128) + 128' ]

value

A subroutine reference that will be called to generate the value used for the text of the captcha. If not set, it defaults to:

sub { int( rand( 10_000_000 - 1_000_000 ) ) + 1_000_000 }

display

A subroutine reference that will be called and passed a value. The subroutine is expected to alter the value for display purposes. For example, adding spaces or dashes or other such things. If not set, it defaults to:

sub {
    my ($display) = @_;
    $display =~ s/^(\d{2})(\d{3})/$1-$2-/;
    $display =~ s/(.)/ $1/g;
    return $display;
}

HELPER METHODS

get_captcha_value

This method (expected to be used in a Mojolicious controller) will return the stored captcha value from the most recent image generation.

my $captcha_value = $app->get_captcha_value;

set_captcha_value

This method (likely never used, but if used would be expected to be used in a Mojolicious controller) will set a captcha value.

$app->set_captcha_value(42);

check_captcha_value

This method (expected to be used in a Mojolicious controller) expects a captcha value and will return true or false if it matches the stored captcha value.

my $success = $app->check_captcha_value($captcha_value);

On success, the captcha value is removed from the session.

clear_captcha_value

Removes the captcha value from the session.

$app->clear_captcha_value;

SEE ALSO

Mojolicious, Mojolicious::Plugin.

You can also look for additional information at:

AUTHOR

Gryphon Shafer <gryphon@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2013-2050 by Gryphon Shafer.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)