php简易验证码

这篇博客介绍了如何使用PHP创建一个带有干扰元素的随机验证码。代码首先定义了验证码区域大小,然后生成了背景色和随机字符串。接着,设置了文字颜色并将字符串写入图片。为了增加干扰,代码绘制了随机线条和像素点。最后,设置了HTTP头并输出为PNG图像。
摘要由CSDN通过智能技术生成
<?php
    $im=imagecreatetruecolor(200,50);//验证码区域大小
    //创建背景色
    $color=imagecolorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
    //填充画布
    imagefill($im,0,0,$color);
    //随机生成验证码字符
    $str='123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    $captcha='';
    for($i=0;$i<5;$i++)
    {
        $captcha .=$str[mt_rand(0,strlen($str)-1)];
    }

    //设置文字颜色
    $str_color=imagecolorallocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
    //将文字写入图片
    imagestring($im,5,80,20,$captcha,$str_color);
    //增加干扰线(要在字上层)
    for($j=0;$j<5;$j++)
    {
    $line_color=imagecolorallocate($im,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));

    //划线
    
    imageline($im,mt_rand(0,200),mt_rand(0,50),mt_rand(0,200),mt_rand(0,50),$line_color);
    }
        //画点
    for($i=0;$i<300;$i++)
        {
        $line_pixel=imagecolorallocate($im,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));

        imagesetpixel($im,mt_rand(0,200),mt_rand(0,50),$line_pixel);
        }
    header('Content-type:image/png');
    imagepng($im);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值