ASP4x-Captcha-Imager

 view release on metacpan or  search on metacpan

lib/ASP4x/Captcha/Imager.pm  view on Meta::CPAN

  my $img = Imager->new(
    xsize => eval { $Config->system->settings->captcha_width }  || 140,
    ysize => eval { $Config->system->settings->captcha_height } || 70
  );

  $img->box(
    filled => 1,
    color  => eval { $Config->system->settings->captcha_bg_color } || 'white'
  );

  my $font = Imager::Font->new( $s->font );

  my @colors = qw(
    A9A9A9  878787  656565  808080
    CACACA  EFEFEF  DEDEDE  CDCDCD
    BABABA  A9A9A9  878787  656565
    434343  212121  EFEFEF  DEDEDE
    CDCDCD  BABABA  CCCCCC  AAAAAA
  );
  
  # Add the word to the image, but make it hard to read:

lib/ASP4x/Captcha/Imager.pm  view on Meta::CPAN

  
  for( 1..10 )
  {
    for my $idx ( 0..@chars - 1 )
    {
      for( 1..int(rand() * @colors) )
      {
      push @colors, shift(@colors);
      }
      my $color = $colors[0];
      $font->align(
        halign  => 'center',
        valign  => 'center',
        string  => $chars[$idx],
        size    => $charHeight,
        image   => $img,
        color   => $color,
        'x'     => ( $idx * $charWidth ) + int(rand() * 8) + $charWidth,
        'y'     => ($img->getheight / 2) + int(rand() * 8) - 4,
      );
    }# end for()

lib/ASP4x/Captcha/Imager.pm  view on Meta::CPAN

  {
    $word .= substr($chars, int(rand()*length($chars)), 1);
  }# end while()
  
  my $key = md5_hex( lc($word) . ( eval { $Config->system->settings->captcha_key } || '' ) );
  
  return ( $word, $key );
}# end generate_pair()


sub font
{
  my $s = shift;
  
  return ( file => $Config->system->settings->captcha_font );
}# end font()

1;# return true:

=pod

=head1 NAME

ASP4x::Captcha::Imager - Imager-based CAPTCHA for your ASP4 web application.

=head1 SYNOPSIS

=head2 In Your asp4-config.conf

  {
    ...
    "system": {
      ...
      "settings": {
        ...
        "captcha_key":      "Some random string of any length",
        "captcha_font":     "@ServerRoot@/etc/LiberationSans-Regular.ttf",
        "captcha_width":    140,
        "captcha_height":   40,
        "captcha_bg_color": "FFFFFF",
        "captcha_length":   4
        ...
      }
    }
  }

=head2 In a handler

lib/ASP4x/Captcha/Imager.pm  view on Meta::CPAN

Any form that might be attacked by a script including registration forms, email forms, etc.
is a good candidate for a Captcha.  Since it's so easy to use Captchas there really
isn't any reason not to use them anywhere you think B<might> benefit.  If you keep
the Captcha length short (see the first point in this list) then the humans won't
be too bothered by them and may actually be pleased with your consideration of their privacy.

=back

=head2 What About Fonts?

Because Linux systems tend to put fonts in several different places, I recommend
copying the font file (*.ttf) into the C<etc/> folder of your website and referencing it
(just like you see in the C<t/> folder of this distribution and in the SYNOPSIS example above.

Mono-space fonts are recommended over variable-width fonts.  So, "Courier New" is
recommended over Verdana.

=head1 SEE ALSO

L<ASP4>, L<Imager>

=head1 PREREQUISITES

L<Imager>, L<ASP4>, L<Digest::MD5>

t/conf/asp4-config.json  view on Meta::CPAN

    ],
    "libs": [
      "@ServerRoot@/lib"
    ],
    "load_modules": [
    ],
    "env_vars": {
    },
    "settings": {
      "captcha_key":      "Captcha-Is-Teh-Awesome",
      "captcha_font":     "@ServerRoot@/etc/LiberationSans-Regular.ttf",
      "captcha_width":    140,
      "captcha_height":   40,
      "captcha_bg_color": "FFFFFF",
      "captcha_length":   4
    }
  },
  "errors": {
    "error_handler":    "ASP4::ErrorHandler",
    "mail_errors_to":   "you@your-server.com",
    "mail_errors_from": "root@localhost",



( run in 0.991 second using v1.01-cache-2.11-cpan-ceb78f64989 )