$im->copyResized($source,($i*($self->width()))+$a,$b,0,0,($self->width())+$c,($self->height())+$d,$self->width(),$self->height());
}
# distort the code graphic
for(my $i=0; $i<($length*($self->width())*($self->height())/14+200); $i++)
{
my $a = (int(rand ($length*($self->width())))+0);
my $b = (int(rand $self->height())+0);
my $c = (int(rand ($length*($self->width())))+0);
my $d = (int(rand $self->height())+0);
my $index = $im->getPixel($a,$b);
if ($i < (($length*($self->width())*($self->height())/14+200)/100))
{
$im->line($a,$b,$c,$d,$index);
} elsif ($i < (($length*($self->width())*($self->height())/14+200)/2)) {
$im->setPixel($c,$d,$index);
} else {
$im->setPixel($c,$d,$black);
}
}
# generate a background
my $a = int(rand 5)+1;
my $background_img = File::Spec->catfile($self->images_folder(),"background" . $a . ".png");
my $source = new GD::Image($background_img);
my ($background_width,$background_height) = $source->getBounds();
my $b = int(rand (int($background_width/13)))+0;
my $c = int(rand (int($background_height/7)))+0;
my $d = int(rand (int($background_width/13)))+0;
my $e = int(rand (int($background_height/7)))+0;
my $source2 = new GD::Image(($length*($self->width())),$self->height());
$source2->copyResized($source,0,0,$b,$c,($length*($self->width())),$self->height(),$background_width-$b-$d,$background_height-$c-$e);
# merge the background onto the image
$im->copyMerge($source2,0,0,0,0,($length*($self->width())),$self->height(),40);
# add a border
$im->rectangle(0,0,((($length)*($self->width()))-1),(($self->height())-1),$black);
# save the image to file
my $png_data = $im->png;
return \$png_data;
}
sub create_sound_file
{
ref(my $self = shift) or croak "instance variable needed";
my $code = shift;
my $md5 = shift;
my $length = length($code);
my @chars = split('',$code);
my $snd_file;
local $/; # input record separator. So we can slurp the data.
# get a random voice speaking the code
foreach my $char (@chars)
{
my $voice = int(rand $num_of_soundfile_versions) + 1;
my $src_name = File::Spec->catfile($self->images_folder(),$voice, $char . ".wav");
warn "Open File: $src_name\n" if($self->debug() >= 2);
open (FILE,"< $src_name") or die "Can't open File: $src_name\n";
flock FILE, 1; # read lock
binmode FILE;
$snd_file .= <FILE>;
close FILE;
warn "Close File: $src_name\n" if($self->debug() >= 2);
}
return \$snd_file;
}
sub _save_file
{
ref(my $self = shift) or croak "instance variable needed";
my $file_ref = shift;
my $file_name = shift;
warn "Open File: $file_name\n" if($self->debug() >= 2);
open (FILE,">$file_name") or die "Can't open File: $file_name \n";
flock FILE, 2; # write lock
binmode FILE;
print FILE $$file_ref;
close FILE;
warn "Close File: $file_name\n" if($self->debug() >= 2);
}
sub generate_code
{
ref(my $self = shift) or croak "instance variable needed";
my $length = shift;
my $code = $self->generate_random_string($length);
my $md5 = md5_hex($code);
my ($captcha_data_ref,$output_filename);
if ($self->type() eq 'image')
{
$captcha_data_ref = $self->create_image_file($code,$md5);
$output_filename = File::Spec->catfile($self->output_folder(),$md5 . ".png");
} elsif ($self->type() eq 'sound') {
=5= |