<?php
class RandCheckCode
{
/*Function name: get_code()
* Function: Get random string
* Parameters:
1. (int)$length = 32 #Random character length
2. (int)$mode = 0 #Random character type,
0 is upper and lowercase English and digits, 1 is a number, 2 is a lowercase letter, 3 is a capital letter,
4 is uppercase and lowercase letters, 5 is uppercase letters and numbers, and 6 is lowercase letters and numbers
*Return: Obtained string
*/
function get_code($length=32,$mode=0)//Get random verification code function
{
switch ($mode)
{
case '1':
$str='123456789';
break;
case '2':
$str='abcdefghijklmnopqrstuvwxyz';
break;
case '3':
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case '4':
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case '5':
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
break;
case '6':
$str='abcdefghijklmnopqrstuvwxyz1234567890';
break;
default:
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
break;
}
$checkstr='';
$len=strlen($str)-1;
for ($i=0;$i<$length;$i++)
{
//$num=rand(0,$len);//Create a random number between 0 and $len
$num=mt_rand(0,$len);//Create a random number between 0 and $len
$checkstr.=$str[$num];
}
return $checkstr;
}
/** Function name: create_check_image()
Function function: generate a picture of a check code
Parameters: $checkcode: Checkcode string
Return Value: Return this picture
*/
function create_check_image($checkcode)//generates a
{
$im=imagecreate(65,22);//Create an image
$black=imagecolorallocate($im,0,0,0);//Background color
$white=imagecolorallocate($im,255,255,255);//Foreground color
$gray=imagecolorallocate($im,200,200,200);
imagefill($im,30,30,$gray);//Perform area filling with $gray color at the coordinates 30,30 of the $im image (0,0, the upper left corner of the image is 0) (that is, the same color as 30,30 points and adjacent points will be filled)
imagestring($im,5,8,3,$checkcode,$white);// Use the $white color to draw the string $checkcode to the coordinates of the image represented by $im (this is the coordinates of the upper left corner of the string, and the upper left corner of the entire image is 0,0). 5 is the font size, and the font can only be 1, 2, 3, 4 or 5. Use built-in fonts
for ($i=0;$i<120;$i++)
{
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im,rand()%70,rand()%30,$randcolor);//Draw a dot on the $randcolor color on the $im image (rand()%70,rand()%30) coordinate (the upper left corner of the image is 0,0)
}
header("Content-type:image/png");
imagepng($im);//Output image to browser or file in PNG format
imagedestroy($im);//Destroy image $im
}
}
/*
$randcode=new RandCheckCode();
$checkstring=$randcode->get_code(5,7);
$image=$randcode->create_check_image($checkstring);
echo $image;
*/
?>