|
// Enable the session first
Session_start ();
// Define the front-end verification code length and width
$ Image_width = 120;
$ Image_height = 40;
$ Characters_on_image = 6;
$ Font = './monofont. ttf ';
// The characters that can be used in the CAPTCHA code.
// Avoid confusing characters (l 1 and I for example)
$ Possible_letters = '23456789bcdfghjkmnpqrstvwxyz ';
$ Random_dots = 10;
$ Random_lines = 30;
$ Captcha_tex_color = "0x142864 ";
$ Captcha_noice_color = "0x142864 ";
// Define the string to generate the verification code
$ Code = '';
$ I = 0;
While ($ I <$ characters_on_image ){
$ Code. = substr ($ possible_letters, mt_rand (0, strlen ($ possible_letters)-1), 1 );
$ I ++;
}
$ Font_size = $ image_height * 0.75;
$ Image = @ imagecreate ($ image_width, $ image_height );
/* Setting the background, text and noise colours here */
$ Background_color = imagecolorallocate ($ image, 255,255,255 );
$ Arr_text_color = hexrgb ($ captcha_text_color );
$ Text_color = imagecolorallocate ($ image, $ arr_text_color ['red'],
$ Arr_text_color ['green'], $ arr_text_color ['blue']);
$ Arr_noice_color = hexrgb ($ captcha_noice_color );
$ Image_noise_color = imagecolorallocate ($ image, $ arr_noice_color ['red'],
$ Arr_noice_color ['green'], $ arr_noice_color ['blue']);
/* Generating the dots randomly in background */
For ($ I = 0; $ I <$ random_dots; $ I ++ ){
Imagefilledellipse ($ image, mt_rand (0, $ image_width ),
Mt_rand (0, $ image_height), 2, 3, $ image_noise_color );
}
/* Generating lines randomly in background of image */
For ($ I = 0; $ I <$ random_lines; $ I ++ ){
Imageline ($ image, mt_rand (0, $ image_width), mt_rand (0, $ image_height ),
Mt_rand (0, $ image_width), mt_rand (0, $ image_height), $ image_noise_color );
}
/* Create a text box and add 6 letters code in it */
$ Textbox = imagettfbbox ($ font_size, 0, $ font, $ code );
$ X = ($ image_width-$ textbox [4])/2;
$ Y = ($ image_height-$ textbox [5])/2;
Imagettftext ($ image, $ font_size, 0, $ x, $ y, $ text_color, $ font, $ code );
/* Show captcha image in the page html page */
Header ('content-Type: image/jpeg '); // defining the image type to be shown in browser widow
Imagejpeg ($ image); // showing the image
Imagedestroy ($ image); // destroying the image instance
// Set the session for verification
$ _ SESSION ['6 _ letters_code '] = $ code;
Function hexrgb ($ hexstr)
{
$ Int = hexdec ($ hexstr );
Return array ("red" => 0xFF & ($ int> 0x10 ),
"Green" => 0xFF & ($ int> 0x8 ),
"Blue" => 0xFF & $ int );
} |