Home >Backend Development >PHP Tutorial >PHP image verification code production (Part 2)_PHP tutorial
Randomly generate numbers and letter codes:
//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 webpage because it needs to generate a verification code because it has errors and cannot be displayed
header("Content-type:image/jpeg"); imagejpeg($image);
?>
Code for drawing interference lines on pictures:
for($i=0;$i<8;$i++)//Draw multiple lines
{
$cg=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//Generate random colors
imageline($im,rand(10,40),0,rand(10,40),20,$cg);
}
Code to draw interference points on pictures:
for($i=0;$i<80;$i++)//Draw multiple points
{
imagesetpixel($im,rand(0,40),rand(0,20),$cg);
}
Write text into image code:
$str=array('我','我',' dear',' dear');//Storage displayed Chinese characters
for($i=0;$i<4;$i++)
{
$sss.=$str[rand(0,3)];//Randomly display Chinese characters
}
//$str=iconv("gb2312","utf-8",$str); //Chinese character encoding conversion, mine doesn’t seem to need it
imagettftext($im,10,0,rand(5,60),rand(5,60),$te,"simhei.ttf",$sss);//
0: font inclination, "simhei.ttf": font style, usually placed in the root directory;
Excerpted from ms.元