PHP generate Captcha pictures from the Getting Started and proficient tutorial _php tutorial

Source: Internet
Author: User
Tags border color set background
In PHP to generate verification code picture is quite simple, because in PHP provides us with the graphics Gd.dll Library, to enable the GD graphics library we just put PHP-GD in front of the php.ini;

Method One

The code is as follows Copy Code

$authnum = ";
$ychar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
$list =explode (",", $ychar);//Split function
for ($i =0; $i <4; $i + +) {
$randnum =rand (0,35);
$authnum. = $list [$randnum];//output as an array

Method Two:

The code is as follows Copy Code
Private Function Createcheckcode ()
{
For (i=0;i codenum;i++)
{
Number = rand (0,2);
Switch (number)
{
Case 0:rand_number = rand (48,57); break;//Digital
Case 1:rand_number = rand (65,90); break;//Capital Letter
Case 2:rand_number = rand (97,122); break;//lowercase Letter
}
$ASC = sprintf ("%c", Rand_number);
$ASC _number = ASC_NUMBER.ASC;
}
return asc_number;
}

Method Three:

The code is as follows Copy Code
Srand (Microtime () *100000);//equivalent to a timer
$string = "abcdefghigklmnopqrstuvwxyz123456789";
for ($i =0; $i <4; $i + +)
{
$new _number.= $string [Rand (0,strlen ($string)-1)];//immediately produces an array
}

Method Four:

The code is as follows Copy Code

for ($i =0; $i <4; $i + +)
{
$rand. =dechex (rand (1,15));//convert decimal to hexadecimal
}


Randomly generated numbers, letters of code:

The code is as follows Copy Code
che.php
Session_Start ();
for ($i =0; $i <4; $i + +)
{
$rand. =dechex (rand (1,15));
}
$_session[' Check_num ']= $rand;
$image =imagecreatetruecolor (50,30);
$BG =imagecolorallocate ($im, 0,0,0);//When using the palette for the first time, the background color
$te =imagecolorallocate ($im, 255,255,255);
Imagestring ($image, 6,rand (0,20), Rand (0,2), $rand, $te);
Ob_clean (); the image "Http://localhost/**.php" appears in the//php Web page because it has to generate a verification code and cannot be displayed because of its own error
Header ("Content-type:image/jpeg"); Imagejpeg ($image);
?>

To draw an interference line code for a picture:

The code is as follows Copy Code
for ($i =0; $i <8; $i + +)//Draw more than one line
{
$CG =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//produce random colors
Imageline ($im, Rand (10,40), 0,rand (10,40), $CG);
}

To draw a code for the image to the point of interference:

The code is as follows Copy Code
for ($i =0; $i <80; $i + +)//Draw multiple points
{
Imagesetpixel ($im, Rand (0,40), Rand (0,20), $CG);
}

Write the text to the image code:

The code is as follows Copy Code

$str =array (' i ', ' I ', ' pro ', ' Pro ');//store displayed Kanji
for ($i =0; $i <4; $i + +)
{
$sss. = $str [Rand (0,3)];//random display of Chinese characters
}

$str =iconv ("gb2312", "Utf-8", $str); Chinese character coding transformation, I don't seem to need
Imagettftext ($im, 10,0,rand (5,60), Rand (5,60), $te, "Simhei.ttf", $sss);//

Finally, we share a complete example with the actual

The code is as follows Copy Code
/**
* Generate Verification Code picture
*
* @param String $word The variable name of the CAPTCHA in session
*/
function Valicode ($word = ' Randcode ') {
Header ("Content-type:image/gif");
$border = 0; Whether to border 1 to: 0 do not
$how = 4; Verify number of code bits
$w = $how *15; Picture width
$h = 18; Picture height
$fontsize = 10; Font size
$alpha = "ABCDEFGHIJKMNPQRSTUVWXYZ"; Verification Code Content 1: Letters
$number = "23456789"; Verification Code Content 2: Digital
$randcode = ""; Validation code string initialization
Srand (Double) microtime () *1000000); Initialize random number Seed
$im = Imagecreate ($w, $h); Create a validation picture
/*
* Draw the basic frame
*/
$bgcolor = Imagecolorallocate ($im, 255, 255, 255); Set Background color
Imagefill ($im, 0, 0, $bgcolor); Fill background color
if ($border)
{
$black = imagecolorallocate ($im, 0, 0, 0); Set Border color
Imagerectangle ($im, 0, 0, $w-1, $h-1, $black);//Draw Border
}

/*
* Random character generation by bit
*/
for ($i =0; $i < $how; $i + +)
{
$alpha _or_number = Mt_rand (0, 1); Letters or numbers?
$str = $alpha _or_number? $alpha: $number;
$which = Mt_rand (0, strlen ($str)-1); which character to take
$code = substr ($str, $which, 1); Take character
$j =! $i? 4: $j +15; Draw character position
$color 3 = imagecolorallocate ($im, Mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100)); Character Color
Imagechar ($im, $fontsize, $j, 3, $code, $color 3); Paint characters
$randcode. = $code; Add a CAPTCHA string to a bit
}

/*
* Remove Comments If you need to add interference
*
* the following for () loop draws the background noise line code
*/
/* +-------------------------------Draw background noise line start--------------------------------------------+ */
for ($i =0; $i <5; $i + +)//Draw background interference line
{
$color 1 = imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); Interference Line Color
Imagearc ($im, Mt_rand ( -5, $w), Mt_rand ( -5, $h), Mt_rand (20,300), Mt_rand (20,200), $color 1); Interference Line
}
/* +-------------------------------Draw background interference line end--------------------------------------+ */

/*
* Remove Comments If you need to add interference
*
* the following for () loop draws the background noise point code
*/
/* +--------------------------------Draw background noise point start------------------------------------------+ */

for ($i =0; $i < $how *40; $i + +)//plot background interference points
{
$color 2 = imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); Interference point Color
Imagesetpixel ($im, Mt_rand (0, $w), Mt_rand (0, $h), $color 2); Interference Point
}

/* +--------------------------------Draw background noise point end------------------------------------------+ */

Write the captcha string to the SESSION to verify that the verification code is correct when you submit the login information for example: $_post[' randcode '] = $_session[' Randcode ')
$_session[$word] = $randcode;
/* End of drawing */
Imagegif ($im);
Imagedestroy ($im);
/* End of drawing */
}

The calling method is also very simple to save the above instance to the img.php file, and then on the page to invoke
The HTML page is as follows

The code is as follows Copy Code

This will enable the verification code to be called.

Add a commit verification verification code is correct

The code is as follows Copy Code

Session_Start ();
if ((Strtoupper ($_post["code")) = = Strtoupper (($_session["Verifycode"])) {
Print ("Verification code is correct,");
}else{
Print ("Captcha error,");
}

?>

This is what we have done from the generation of CAPTCHA images and use of the entire process, but also from the beginning of PHP to proficient verification code is all said.

http://www.bkjia.com/PHPjc/631625.html www.bkjia.com true http://www.bkjia.com/PHPjc/631625.html techarticle in PHP to generate verification code picture is quite simple, because in PHP provides us with the graphics Gd.dll Library, to enable the GD graphics library we just put PHP-GD in front of the php.ini;

  • Related Article

    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.