验证码 字母与数字

<?php 
session_start();
header("Content-type:image/PNG");
$img = imagecreate(150,45);
$back = imagecolorallocate($img, 245, 245, 245);
imagefill($img, 0, 0, $back);
$vcodes = "";
$authnum = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
for ($i=0;$i<4;$i++)
{
    $font = imagecolorallocate($img, rand(100,225), rand(0,100), rand(100,255));
    /*显示数字
     * $authnum = rand(0,9);
    $vcodes .= $authnum;
    imagestring($img, 5, 50+$i*10, 20, $authnum, $font);
    */
    //显示字母
    $vcodes .= $authnum[rand(0,count($authnum)-1)];
}
$_SESSION['VCODE'] = $vcodes;
imagettftext($img, 30, rand(-5,5), 5+$i*10, 30, $font, 'MATURASC.ttf', $vcodes);
for ($i=0;$i<200;$i++)
{
    $randcolor = imagecolorallocate($img, rand(0,225), rand(0,225), rand(0,225));
    imagesetpixel($img, rand()%150, rand()%150, $randcolor);
}
imagepng($img);
imagedestroy($img);
?>

猜你喜欢

转载自blog.csdn.net/Black_Fox123/article/details/81708433