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;
        }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM