Note: The following code needs to open the gd library of the php tutorial, modify the configuration of the php. in file, and cancel the semicolon before the commented-out line: extension = php_gd2.dll.
$ Width = 165;
$ Height = 120;
$ Image = imagecreatetruecolor ($ width, $ height );
$ Bg_color = imagecolorallocate ($ image, 255,255,255 );
$ Tm = imagecolorallocate ($ image, 255, 0, 0 );
Imagefilledrectangle ($ image, 0, 0, $ width, $ height, $ bg_color );
Imagefill ($ image, 0, 0, $ bg_color );
/*
// $ Text = random_text (5 );
$ Text = "ffff ";
$ Font = 8;
$ Struft = iconv ("gbk", "UTF-8", $ text );
$ X = imagesx ($ image );
$ Y = imagesy ($ image );
$ Fg_color = imagecolorallocate ($ image, 233,14, 91 );
Imagestring ($ image, $ font, $ x, $ y, $ struft, $ fg_color );
$ _ Session ['captcha '] = $ text;
*/
Header ("content-type: image/png ");
Imagepng ($ image );
Imagedestroy ($ image );
Instance 2
Validate. php
The session recognition is used to slightly improve the php Verification Code currently circulating on the network, add the miscellaneous, and display the numbers in random color to control the display of four digits;
<?
Session_start ();
// Generate a verification code Image
Header ("content-type: image/png ");
$ Im = imagecreate (44,18 );
$ Back = imagecolorallocate ($ im, 245,245,245 );
Imagefill ($ im, 0, 0, $ back); // background
Srand (double) microtime () * 1000000 );
// Generate four digits
For ($ I = 0; $ I <4; $ I ++ ){
$ Font = imagecolorallocate ($ im, rand (100,255), rand (0,100), rand (100,255 ));
$ Authnum = rand (1, 9 );
$ Vcodes. = $ authnum;
Imagestring ($ im, 5, 2 + $ I * 10, 1, $ authnum, $ font );
}
For ($ I = 0; $ I <100; $ I ++) // Add interference pixels
{
$ Randcolor = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor );
}
Imagepng ($ im );
Imagedestroy ($ im );
$ _ Session ['vcode'] = $ vcodes;
?>
The following describes the complete instance.
<? Php
@ Header ("content-type: text/html; charset = UTF-8 ");
// Open the session
Session_start ();
?>
<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
<Title> php verification code example </title>
</Head>
<Body>
Verification Code: <br/>
<Iframe id = "iimg" height = "100" width = 300 src = "img. php" frameborder = "0"> </iframe>
<Br/>
<Input type = button value = "cannot see clearly, change to" onclick = "iimg. location. reload ();">
<Br>
<Form action = "validate. php" method = "post">
Enter the verification code: <input name = "imgid" style = "width: 60">
<Input type = "submit" value = "OK">
</Form>
</Body>
</Html>
Php checks whether the verification code entered by the user is consistent with that generated by the system.
<? Php @ header ("content-type: text/html; charset = UTF-8 ");
// Enable session
Session_start ();
// Obtain the verification code entered by the user and convert it to uppercase.
$ Imgid_req =$ _ request ['imgid'];
$ Imgid_req = strtoupper ($ imgid_req );
// Verify whether the session variable is registered for the string
If (session_is_registered ($ imgid_req )){
Echo "<font color = blue> Verification passed! </Font> ";
} Else {
Echo "<font color = red> verification error! </Font> ";
}
// Close the session to clear all registered Variables
Session_destroy ();
?>