tp5 生成二维码 (原生的 不用composer安装)

html页面

 <img src="/wap/goods/verifty" onClick="this.src='/wap/goods/verifty?nocache='+Math.random()" style="cursor:hand" alt="点击换一张"/>点击图片可更换验证码

在控制器中:

public function verifty(){

        session_start();//必须位于脚本的最顶端
        $image = imagecreatetruecolor(100, 30);//imagecreatetruecolor函数建一个真彩色图像
        //生成彩色像素
        $bgcolor = imagecolorallocate($image, 255, 255, 255);//白色背景     imagecolorallocate函数为一幅图像分配颜色
        $textcolor = imagecolorallocate($image, 0, 0, 255);//蓝色文本
        //填充函数,xy确定坐标,color颜色执行区域填充颜色
        imagefill($image, 0, 0, $bgcolor);
        $captch_code = "";//初始空值

        //该循环,循环取数
        for ($i = 0; $i < 4; $i++) {
            $fontsize = 6;
            $x = ($i * 25) + rand(5, 10);
            $y = rand(5, 10);//位置随机
            //  $fontcontent=$i>2?chr(rand(97,122)):chr(rand(65,90));//是小写,否则是大写
            $data = 'abcdefghijkmnpqrstuvwxyz23456789';
            $fontcontent = substr($data, rand(0, strlen($data) - 1), 1);//strlen仅仅是一个计数器的工作  含数字和字母的验证码
            //可以理解为数组长度0到30


            $fontcolor = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));//随机的rgb()值可以自己定

            imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); //水平地画一行字符串
            $captch_code .= $fontcontent;
        }
        $_SESSION['authcode'] = $captch_code;//将变量保存再session的authcode变量中


        //该循环,循环画背景干扰的点
        for ($m = 0; $m <= 600; $m++) {

            $x2 = rand(1, 99);
            $y2 = rand(1, 99);
            $pointcolor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
            imagesetpixel($image, $x2, $y2, $pointcolor);// 水平地画一串像素点
        }

        //该循环,循环画干扰直线
        for ($i = 0; $i <= 10; $i++) {
            $x1 = rand(0, 99);
            $y1 = rand(0, 99);
            $x2 = rand(0, 99);
            $y2 = rand(0, 99);
            $linecolor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
            imageline($image, $x1, $y1, $x2, $y2, $linecolor);//画一条线段

        }
        header('content-type:image/png');
        imagepng($image);
        //销毁
        imagedestroy($image);
    }

在php中验证

 if (strtolower($_REQUEST['number']) != $_SESSION['authcode']) {//strtolower转化为小写的函数
                echo “fail”
            } else {
               echo "ok";
            }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值