tp5 thinkphp 使用phpqrcode生成带Logo的二维码

1 下载生成二维码类库

composer require aferrandini/phpqrcode

2 点击按钮下载

 //二维码下载
        public function down_qrcode()
        {
            if($this->request->isPost()){
                $shop_id = input('shop_id');
                $merchant_id = Db::table('xcf_shops')->where('shop_id',$shop_id)->value('merchant_id');
                if($merchant_id){
                    $url = 'xcf'.$merchant_id; //二维码内容
                    $pic =  $this->scerweima1($url);
                    return $pic;
                }else{
                    return json(['s' => 'error', 'msg' => 'merchant_id不存在,请进行富友审核']);
                }
            }

        }

//生成二维码
        //2. 在生成的二维码中加上logo(生成图片文件)
        public function scerweima1($text){
            //echo 'hee';die;
           // dump(APP_XCF_LOG.'xcf.png');die;
            //Vendor('chillerlan.php-qrcode.public.qrcode');
            //$pathname = date("Ymd",time());
            $pathname = APP_PATH . '/../public/upload/qrcode/';
            if(!is_dir($pathname)) { //若目录不存在则创建之
                mkdir($pathname,0777,true);
            }

            $errorCorrectionLevel = 'H';  //容错级别
            $matrixPointSize = 10;      //生成图片大小
            //生成二维码图片
            $abc = date("Ymd",time()).time().'.png';
            $filename = $pathname.$abc;
            \PHPQRCode\QRcode::png($text,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
            $logo = APP_PATH . '/../public/assets/xcf_log/xcf.png'; //准备好的logo图片
           // dump(APP_XCF_LOG.'xcf.png');die;
            //dump($logo);die;
            $QR = $filename;      //已经生成的原始二维码图
           // dump($QR);
            if (file_exists($logo)) {
                $QR = imagecreatefromstring(file_get_contents($QR));    //目标图象连接资源。
                $logo = imagecreatefromstring(file_get_contents($logo));  //源图象连接资源。
                $QR_width = imagesx($QR);      //二维码图片宽度
                $QR_height = imagesy($QR);     //二维码图片高度
                $logo_width = imagesx($logo);    //logo图片宽度
                $logo_height = imagesy($logo);   //logo图片高度
                $logo_qr_width = $QR_width / 4;   //组合之后logo的宽度(占二维码的1/5)
                $scale = $logo_width/$logo_qr_width;  //logo的宽度缩放比(本身宽度/组合后的宽度)
                $logo_qr_height = $logo_height/$scale; //组合之后logo的高度
                $from_width = ($QR_width - $logo_qr_width) / 2;  //组合之后logo左上角所在坐标点
                //重新组合图片并调整大小
                /*
                 * imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
                 */
                imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
            }
            //输出图片
            imagepng($QR,$filename);
            
            return request()->domain().'/upload/qrcode/'.$abc;
        }

猜你喜欢

转载自www.cnblogs.com/yehuisir/p/11429908.html