First, the display effect
Second, the code is as follows
Copy CodeThe code is as follows:/*
* @Author FY
*/
$imgwidth = 100; Picture width
$imgheight = 40; Picture height
$codelen = 4; Verification Code length
$fontsize = 20; Font size
$charset = ' abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789 ';
$font = ' Fonts/segoesc.ttf ';
$im =imagecreatetruecolor ($imgwidth, $imgheight);
$while =imagecolorallocate ($im, 255,255,255);
Imagefill ($im, 0,0, $while); Fill image
Get string
$authstr = ";
$_len = strlen ($charset)-1;
for ($i =0; $i < $codelen; $i + +) {
$authstr. = $charset [Mt_rand (0,$_len)];
}
Session_Start ();
$_session[' Scode ']=strtolower ($AUTHSTR);//All lowercase, mainly for case insensitive
Random draw points, has been changed to draw the stars
for ($i =0; $i < $imgwidth; $i + +) {
$randcolor =imagecolorallocate ($im, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Imagestring ($im, Mt_rand (1,5), Mt_rand (0, $imgwidth), Mt_rand (0, $imgheight), ' * ', $randcolor);
Imagesetpixel ($im, Mt_rand (0, $imgwidth), Mt_rand (0, $imgheight), $randcolor);
}
Randomly draw lines, number of lines = number of characters (whatever)
for ($i =0; $i < $codelen; $i + +)
{
$randcolor =imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));
Imageline ($im, 0,mt_rand (0, $imgheight), $imgwidth, Mt_rand (0, $imgheight), $randcolor);
}
$_x=intval ($imgwidth/$codelen); Calculating character distances
$_y=intval ($imgheight *0.7); The character is displayed in the position of picture 70%
for ($i =0; $i<>< p=""><>
$randcolor =imagecolorallocate ($im, Mt_rand (0,150), Mt_rand (0,150), Mt_rand (0,150));
Imagestring ($im, 5, $j, 5, $imgstr [$i], $color 3);
Imagettftext (Resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, String $text)
Imagettftext ($im, $fontsize, Mt_rand ( -30,30), $i *$_x+3,$_y, $randcolor, $font, $authstr [$i]);
}
Generate images
Header ("Content-type:image/png");
Imagepng ($im);
Imagedestroy ($im);