#!/usr/bin/perl --
use File::Spec;
use Image::Magick;
my $imbase = Image::Magick->new(magick=>'png') or die("Image creation failed.");
# Read the predefined letter PNG for each letter
my $x = $imbase->Read(map { File::Spec->catfile('/my/web/httpd/htdocs/mt/images/captcha-source/',$_.'.png') } (a..z));
if ($x) {
die($x);
}
####
require Image::Magick;
my $imbase = Image::Magick->new(magick=>'png')
or return $app->error($app->translate("Image creation failed."));
# Read the predefined letter PNG for each letter in $code
my $x = $imbase->Read(map { File::Spec->catfile($base, $_ . '.png') } split(//, $code));
if ($x) {
return $app->error($app->translate("Image error: [_1]", $x));
}
####
sub _generate_captcha {
my $self = shift;
my ($app, $code, $format) = @_;
$format ||= 'png';
my $len = LENGTH();
my $cfg = $app->config;
my $base = $cfg->CaptchaSourceImageBase;
unless ($base) {
require File::Spec;
$base = File::Spec->catfile(MT->instance->config_dir, 'mt-static', 'images', 'captcha-source');
$base = undef unless (-d $base);
}
return $app->error($app->translate('You need to configure CaptchaSourceImageBase.'))
unless $base;
require Image::Magick;
my $imbase = Image::Magick->new(magick=>'png')
or return $app->error($app->translate("Image creation failed."));
# Read the predefined letter PNG for each letter in $code
my $x = $imbase->Read(map { File::Spec->catfile($base, $_ . '.png') }
split(//, $code));
if ($x) {
return $app->error($app->translate("Image error: [_1]", $x));
}
# Futz with the size and blurriness of each letter
foreach my $i (0..($len - 1)) {
my $a = int rand int(WIDTH() / 14);
my $b = int rand int(HEIGHT() / 12);
$imbase->[$i]->Resize(width => $a, height => $b, blur => rand(3));
}
# Combine all the individual tiles into one block
my $tile_geom = join('x', $len, 1);
my $geometry_str = join('x', WIDTH(), HEIGHT());
my $im = $imbase->Montage(geometry => $geometry_str,
tile => $tile_geom);
$im->Blur();
# Add some lines and dots to the image
for my $i (0..($len * WIDTH() * HEIGHT() / 14+200-1)) {
my $a = int rand($len * WIDTH());
my $b = int rand HEIGHT();
my $c = int rand($len * WIDTH());
my $d = int rand HEIGHT();
my $index = $im->Get("pixel[$a, $b]");
if ($i < ($len * WIDTH() * HEIGHT() / 14+200) / 100) {
$im->Draw(primitive => 'line',
stroke => $index,
points => "$a, $b, $c, $d");
} elsif ($i < ($len * WIDTH() * HEIGHT() / 14+200) / 2) {
$im->Set("pixel[$c, $d]" => $index);
} else {
$im->Set("pixel[$c, $d]" => "black");
}
}
# Read in the background file
my $a = int rand(5) + 1;
my $background = Image::Magick->new();
$background->Read(File::Spec->catfile($base, 'background' . $a . '.png'));
$background->Resize(width => ($len * WIDTH()), height => HEIGHT());
$im->Composite(compose => "Bumpmap",
tile => 'False',
image => $background);
$im->Modulate(brightness => 105);
$im->Border(fill => 'black',
width => 1,
height => 1,
geometry => join('x', WIDTH() * $len, HEIGHT()));
my @blobs = $im->ImageToBlob(magick=>$format);
return $blobs[0];
}