Php: several methods to generate a graphic verification code _ PHP Tutorial

Source: Internet
Author: User
Summary of several methods to generate a graphic verification code using php. To generate a graphic verification code, you must use the phpGD library to generate it. in the INI file, find extensionphp_gd2.dll and remove the previous one. then, restart apache or use the php GD library to generate the graphic verification code. if you have not opened an account for the GD library, we need to go to php. in the INI file, find extension = php_gd2.dll and remove the previous one. then, restart the apache or iis environment.

Let's first check whether your php has opened the gd Library.

The code is as follows:

If (extension_loaded ('gd ')){
Echo 'you can use gd
';
Foreach (gd_info () as $ cate => $ value)
Echo "$ cate: $ value
";
} Else
Echo 'You have not installed the gd extension ';
?>

If the returned information is correct, it can be used frequently.


Example 1

The code is as follows:

/**
* The value of vCode (m, n, x, y) m is n-side width x-side height y.
* Record the session $ code by yourself
*/
Session_start ();
VCode (4, 15); // 4 digits. the display size is 15.

Function vCode ($ num = 4, $ size = 20, $ width = 0, $ height = 0 ){
! $ Width & amp; $ width = $ num * $ size * 4/5 + 5;
! $ Height & $ height = $ size + 10;
// Remove 0 1 O l and so on
$ Str= "23456789 abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW ";
$ Code = '';
For ($ I = 0; $ I <$ num; $ I ++ ){
$ Code. = $ str [mt_rand (0, strlen ($ str)-1)];
}
// Draw an image
$ Im = imagecreatetruecolor ($ width, $ height );
// Define the color to be used
$ Back_color = imagecolorallocate ($ im, 235,236,237 );
$ Boer_color = imagecolorallocate ($ im, 118,151,199 );
$ Text_color = imagecolorallocate ($ im, mt_rand (0,200), mt_rand (0,120), mt_rand (0,120 ));
// Draw the background
Imagefilledrectangle ($ im, 0, 0, $ width, $ height, $ back_color );
// Draw a border
Imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ boer_color );
// Draw interference lines
For ($ I = 0; $ I <5; $ I ++ ){
$ Font_color = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagearc ($ im, mt_rand (-$ width, $ width), mt_rand (-$ height, $ height), mt_rand (30, $ width * 2), mt_rand (20, $ height * 2), mt_rand (0,360), mt_rand (0,360), $ font_color );
}
// Draw interference points
For ($ I = 0; $ I <50; $ I ++ ){
$ Font_color = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ font_color );
}
// Draw the verification code
@ Imagefttext ($ im, $ size, 0, 5, $ size + 3, $ text_color, 'C: \ WINDOWS \ Fonts \ simsun. ttc ', $ code );
$ _ SESSION ["VerifyCode"] = $ code;
Header ("Cache-Control: max-age = 1, s-maxage = 1, no-cache, must-revalidate ");
Header ("Content-type: image/png; charset = gb2312 ");
Imagepng ($ im );
Imagedestroy ($ im );
}

?>

Example 2

A verification code generation example (recommended by w3c) developed using PHP combined with session and GD Library extension can be easily used in projects. And beautiful style // first enable session
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 );
}

I personally recommend that you use the second program code to generate the verification code. you can refer to the comparison. The last one is generated by W3C standards and the php gd Library.

Generate the GD Library. if you have not opened an account for the GD library, we need to find extension = php_gd2.dll in the php. ini file to remove the previous one. then, restart apache or...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.