CAPTCHA Perl. Generation with ImageMagick
A small generator function captcha for perl imagemagick by calling through the library Image:: Magick.
May come in handy when the hoster does not allow to install your own modules for perl (eg GD:: SecurityImage or Auchten:: Captcha). Module to work with ImageMagick should not default unless a very negligent host.
This feature can be used as a library call, the call to pass the file name and line to generate a CAPTCHA.
#! / Usr / bin / perl # Generator captcha. The idea and the Convert call parameters are taken from captcha.ru (http://captcha.ru/captchas/multiswirl/). # Implementing a library Image:: Magick for perl - dimio (www.dimio.org). # 27.09.2009 use Image:: Magick; sub CreateCapImage ($$){ my ($ cap_string, $ filename) = @ _; my $ font = 'times.ttf'; my $ pointsize = 70; my $ path = ' . / '; my $ image = new Image:: Magick; # 1. Create a 300x100 white box. $ Image-> Set (size => '300x100 '); $ image-> ReadImage (' xc: white '); # 2. We print in black with antialiasing $ image-> Set (type => 'TrueColor', antialias => 'True', fill => 'black', # string STRING font $ font size $ pointsize font => $ font, pointsize => $ pointsize,); $ image-> Draw (primitive => 'text', points => '20, 70 ', # orientation lines of text inside the picture text => $ cap_string, # that print) # 3. Move the center to the left at 100 dots + random fluctuation $ image-> Extent (geometry => '400x120 ', # change the image size); $ image-> Roll (x => 101 + int (rand (4))) # 4. The first swirl angle at random (from 37 to 51) $ image-> Swirl (degrees => int (rand (14)) 37,), # 5. Move the center to the right by 200 pixels, also with a random fluctuation of $ image-> Extent (geometry => '600x140 ', # change the image size); $ image-> Roll (x => 3-int (rand (4))) , # 6. The second rotation (from 20 to 35) $ image-> Swirl (degrees => int (rand (15)), 20,,), # 7. Final processing and output $ image-> Crop ('300x100 +100 +17'); $ image-> Resize ('150x50 '); $ filename = $ path. $ Filename; $ filename .= '. Png'; open (IMAGE ,'>',$ filename) or die $!; $ Image-> Write (file => \ * IMAGE, filename => $ filename); close ( IMAGE); return $ filename;} 1;
# Call script of CAPTCHAs my $ num1 = int (rand (11)) + int (rand (3)); my $ num2 = int (rand (8)) + int (rand (4)); my $ sum = $ num1 + $ num2; my $ cap_string = $ num1 .'+'.$ num2 .'='; my $ cap_digest = md5_hex ($ sum + rand (100) + rand (50)); my $ cap_url = & CreateCapImage ($ cap_string, $ cap_digest); $ Cap_url = ~ s | / home / dimioorg / public_html / dimioorg | |; $ Cap_url = 'http://www.dimio.org'. $ Cap_url; print $ query-> em ("<img src=\"$cap_url\">"), $ Query-> textfield ( -Name => 'cap_value', -Size => 2, -Maxlength => 2, );
As a result, the page will display the picture with symbols of the form "12 7 =". The string can in principle be anything.
Too lazy to copy-paste - can download feature captcha.pl and three ttf font .
Written on the Rights of the sights for yourself, but if someone come in handy ![]()
More on similar topics:
Filed under: Internet , Coding |
Tags: captcha , How-to , imagemagick , perl , programming |
10 comments 

Something does not work!
Picture - just a white rectangle with no text, and then
waddle on
print $ query-> em (""),
$ Query-> textfield (
-Name => 'cap_value',
-Size => 2,
-Maxlength => 2,
);
with a message
Can't call method "em" on an undefined value at / home / ... ....
In the example shown as follows:
And you pass an empty string.
CGI module is connected by the way?
As for the drawing - in the first place - the font is correct? Secondly - you can twist the generation parameters. I obtained a normal CAPTCHA image with the indicated parameters in the example.
Clearly, this cuts the engine code ...
Made as follows:
$ Query = new CGI;
print $ query-> em (""),
$ Query-> textfield (
-Name => 'cap_value',
-Size => 10,
-Maxlength => 5,
);
everything worked, but the picture is empty
img src = \ "$ cap_url \" border = \ "2 \"
Launched to test the server host: - .
Perl v5.8.8
ImageMagick 6.5.9-9 2010-02-25 Q16
Image:: Magick (6.005009)
so did a captcha ... it works.
and how to properly check its input and the user?
I would have figured a hash generated CAPTCHA words and a hash of user input and then compared them. Module Digest:: MD5 to help.
Well the user input to calculate the hash with no problems ... but as a hash captcha? he's already forgotten, because worked on the script output captcha.) Yes, and just do it different scripts can be - one displays the captcha, and one that takes the input ... here and here zamorochki I (
Hash captcha put in a file or another database (a file more logical and easier, but if you already have a database for the engine - why not create a table there), spent (and if you want - foul) hashes captures deleted. For convenience, you can assign certain identifiers CAPTCHA.