verification code

<?php
//Draw verification code (generate)
$num=4; //Length of verification code
$str = getCode($num,0);// Use the following custom function to get the required verification code value
session_start();
$_SESSION['res'] = $str;
 
//1. Create a canvas and assign colors
$width=$num*20;//Width
$height=30;//height
$im = imagecreatetruecolor($width,$height);//Create a canvas
//Define several colors (output verification codes of different colors)
$color[] = imagecolorallocate($im,111,0,55);
$color[] = imagecolorallocate($im,0,77,0);
$color[] = imagecolorallocate($im,0,0,160);
$color[] = imagecolorallocate($im,221,111,0);
$color[] = imagecolorallocate($im,220,0,0);
$bg = imagecolorallocate($im,240,240,240);//background
//2. Start painting
imagefill($im,0,0,$bg);
imagerectangle($im,0,0,$width-1,$height-1,$color[rand(0,4)]);
 
//Add random interference points
for($i=0;$i<200;$i++){
$c = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//Random a color
imagesetpixel($im,rand(0,$width),rand(0,$height),$c);
}
 
// //Randomly add interference lines
// for($i=0;$i<5;$i++){
// $c = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//Random a color
// imageline($im,rand(0,$width),rand(0,$height),rand(0,$width),rand(0,$height),$c);
// }
 
//Draw the CAPTCHA content (drawing one character by one):
for($i=0;$i<$num;$i++){
imagettftext($im,20,rand(-40,40),8+(18*$i),24,$color[rand(0,4)],"./simsun.ttf",$str[$i]);
}
// 3. Output image
header("Content-Type:image/png");//Set the response header information (note that there can be no output before this function is executed)
imagepng($im);
//4. Destroy the image (release the content)
imagedestroy($im);
 
 
/**
* A function that randomly generates the content of a verification code
* @param $m : the number of verification codes (default is 4)
* @param $type : the type of verification code: 0: pure numbers 1: numbers + lowercase letters 2: numbers + uppercase and lowercase characters
*/
function getCode($m=4,$type=0){
$str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$t = array(9,35,strlen($str)-1);
/ / Randomly generate the required content of the verification code
$c="";
for($i=0;$i<$m;$i++){
$c.=$str[rand(0,$t[$type])];
}
return $c;
}
 
// echo getCode();
?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325019202&siteId=291194637