PHP绘制验证码

<?php
    
    //使用PHP绘图技术,画出自己的验证码

    $checkCode="";
    for($i=0;$i<4;$i++){
        
        //dechex把一个十进制数转换成十六进制
        $checkCode.=dechex(rand(1,15));
    }

    //存入到session
    session_start();
    $_SESSION['checkcode']=$checkCode;

    //创建画布
    $image1=imagecreatetruecolor(110,30);

    //创建颜色
    $white=imagecolorallocate($image1,255,255,255);
    
    //绘制字符
    imagestring($image1,rand(1,5),rand(0,80),rand(0,20),$checkCode,$white);
    
    //画出干扰线
    //创建干扰线随机颜色
    for($i=0;$i<20;$i++){
        $randomColor=imagecolorallocate($image1,rand(0,255),rand(0,255),rand(0,255));
        imageline($image1,rand(0,110),rand(0,30),rand(0,110),rand(0,30),$randomColor);
    }
    //输出
    header("content-type:image/png");
    imagepng($image1);

    //销毁图片
    imagedestroy($image1);
?>

图片形式是这样的

posted @ 2019-02-27 17:41  张大球哈哈  阅读(545)  评论(4编辑  收藏  举报