Use php to create verification code technology

Source: Internet
Author: User
This function is a string required to generate a verification code. Principle: First, we integrate letters and numbers, then use the str_shuffle function to disrupt the string, and then extract the required length to return .? Php ** function Description: This function returns different types or strings of different lengths to create a verification code *** function

This function is a string required to generate a verification code. Principle: first integrate letters and numbers, then use the str_shuffle function to disrupt the string, and then extract the required length to return. ? Php/** function Description: This function is used to return different types or strings of different lengths for verification code ***/function

This function is a string required to generate a verification code. Principle: first integrate letters and numbers, then use the str_shuffle function to disrupt the string, and then extract the required length to return.

/*
* Function Description: This function returns different types or strings of different lengths to create verification codes.
*
**/
Function buildRandomString ($ type = 1, $ length = 4)
{
If ($ type = 1)
{
// Pure numeric verification code. join converts the array to the meaning of the range distribution of the string.
$ Chars = join ("", range (0, 9 ));
}
Else if ($ type = 2)
{
// Array_merge () combines several arrays into one
$ Chars = join ("", array_merge (range ("a", "z", range ("A", "Z "))));
}
Else if ($ type = 3)
{
$ Chars = join ("", array_merge (range ("a", "z", range ("A", "Z"), range (0, 9 ))));
}
If ($ length> strlen ($ chars ))
{
Exit ("too long ");
}
// Disrupt the string Function
$ Chars = str_shuffle ($ chars );
Return substr ($ chars, 0, $ length );
}

?>

The part used to create the verification code

// Create a canvas
Require_once 'string. func. php ';
Header ("Content-type: image/png ");
$ Width = 180;
$ Height = 28;
// Set the color
$ Image = imagecreatetruecolor ($ width, $ height );
$ White = imagcolorallocate ($ image, 255,255,255 );
$ Black = imagecolorallocate ($ image, 0, 0 );
Imagefilledrectangle ($ image, 1,1, $ width-2, $ height-2, $ black );
// ImageFill ($ image, 0, 0, $ white );
// ImageLine ($ image, 10, 10, 90, 40, $ black );
// The type is 1 and the length is 8
$ Type = 1;
$ Length = 8;
// Set the verification code based on the type and length
$ Chars = buildRandomString ($ type, $ length );
$ Sess_name = "verify ";
$ _ SESSION [$ sess_name] = $ chars;
// Create the font file required for the verification code
$ Fontfiles = array ("MSYH. TTF "," MSYHBD. TTF "," SIMFANG. TTF "," SIMHEI. TTF "," SIMKAI. TTF "," SIMSUN. TTC ");
For ($ I = 0; $ I <$ length; $ I ++)
{
// You can specify the size, angle, and font type.
$ Size = mt_rand (14,18 );
$ Angle = mt_rand (-15, 15 );
$ X = 5 + $ I * $ size;
$ Y = mt_rand (20, 26 );
$ Fontfile = "../fonts/". $ fontfiles [mt_rand (0, count ($ fontfiles)-1)];
$ Color = imagecolorallocate ($ image, mt_rand (50, 90), mt_rand (80,200), mt_rand (90,100 ));
$ Text = substr ($ chars, $ I, 1 );
Imagettftext ($ image, $ size, $ angle, $ x, $ y, $ color, $ fontfile, $ text );
}
Imagepng ($ image );
// Remember to release the resource
ImageDestroy ($ image );
?>

The final result shows the type and font verification code of the required length.

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.