|
|
OverviewGenerate a CAPTCHA security image. For authentication, refer to Authenticating an input based on a security image.
Flow
A sample code
use GD::SecurityImage::AC;
my $captcha = GD::SecurityImage::AC->new;
$captcha->gdsi(
new => {
width => 230,
height => 80,
lines => 10,
scramble => 1,
font => "cour.ttf",
ptsize => 30,
},
create => ['ttf', 'rect', '#000000', '#555555'],
particle => [500],
);
my $capdatadir = "capdata";
$captcha->data_folder($capdatadir);
$captcha->output_folder($capdatadir);
$captcha->expire(5 * 60);
my $md5sum = $captcha->generate_code(4);
print "Content-Type: text/html\n\n";
print "<img src=\"$capdatadir/$md5sum.png\">\n";
Description of the codeuse GD::SecurityImage::AC; Load GD::SecurityImage::AC. This module is original developped to be replaced from Authen::Captcha and according to the document of this module, it recommends to use GD::SecurityImage directly. However, since it is too convenient to handle GD::SecurityImage through GD::SecurityImage::AC, this sample uses it. my $captcha = GD::SecurityImage::AC->new; Generates an object of GD::SecurityImage::AC.
$captcha->gdsi(
new => {
width => 230,
height => 80,
lines => 10,
scramble => 1,
font => "cour.ttf",
ptsize => 30,
},
create => ['ttf', 'rect', '#000000', '#555555'],
particle => [500],
);
Pass the parameters to GD::SecurityImage though the gdsi method. The width value is the width of the generating image and height is the height. The lines is the number of lines to be inserted in the image. You can adjust the nember based on the image size. Scramble is to enable the scramble mode. 1 means enable. If the scramble mode is enabled, the characters are spread out in the image. Therefore, you need to make the image bigger. Otherwise, the character will located out of the image. The font is a True Type font name to be used. According to the document, this has to be specified as absolute path. However, it seems work even with relative path. The create is the setting for the actual image. It has four arguments as follows. $captcha->create($method, $style, $text_color, $line_color); $method could be normal or ttf. In normal, it uses non-True Type font. In ttf, it use a True Type font. $style spcifies background image and the following types can be selected.(Old GD may not be able to use all of them)
$text_color is the color of the letter, $line_color is the color of the line. particle is density of the dot to be spread out in the image. my $capdatadir = "capdata"; $captcha->data_folder($capdatadir); $captcha->output_folder($capdatadir); $captcha->expire(5 * 60); Specify more parameters. The directory to store the images and the database. In this example, both directories are specified as my $md5sum = $captcha->generate_code(4); The generate_code method generates a security image. The argument indicates the number of the latter. $md5sum which is the return value contains encrypted 128-bit MD5 hash value. The generated image file name becomes $md5sum.png. This $md5sum value will be used in authentication time. Therefore this value must be passed to the next process through <input type=hidden> tag. For authentication, refer to Authenticating an input based on a security image.. print "Content-Type: text/html\n\n"; print "<img src=\"$capdatadir/$md5sum.png\">\n"; Display generated image. The image resides in the directory $capdatadir and file name is $md5sum.png. The IMG tag is used to display it. In order to pass necessary parameters into an authentication script, the following input form needs to be provided. print "<form action=\"capcheck.cgi\" method=\"post\">\n"; print "<img src=\"$capdatadir/$md5sum.png\"><br>\n"; print "<input type=\"hidden\" name=capmd5 value=\"$md5sum\">\n"; print "Please entery the above letters into the following text box.<br>"; print "<input type=\"text\" name=intext size=10><p>\n"; print "</form>\n"; In this example, the authentication script is called capcheck.cgi. |