Thinkphp3.2結合phpqrcode生成二維碼(含Logo的二維碼),附案例


首先,下載phpqrcode,將其解壓到項目ThinkPHP\Library\Vendor目錄下。
Index_index.html(模板可自行配置)

<form action="{:U('index/index')}" enctype="multipart/form-data"  method = "post">
    <b>尺寸大小:</b>
        <input type="text" name = "matrixPointSize">    <br>
    <b>邊距大小:</b>
        <select class="form-control" name="matrixMarginSize" >
            <option value="1" selected="selected">1px</option>
            <option value="2">2px</option>
            <option value="3">3px</option>
            <option value="4">5px</option>
        </select><br>
    <b>容錯級別:</b>
        <select class="form-control" name="errorCorrectionLevel" id="errorCorrectionLevel">
        <option value="L">Low (7%)</option>
        <option value="M" selected="selected">Medium (15%)</option>
        <option value="Q">Quartile (25%)</option>
        <option value="H">High (30%)</option>
        </select><br>
    <b>二維碼內容:</b>
        <input type="text" name = "content">    <br>
    <b>二維碼logo:</b>
        <input type="file" name = "test" /><br><br>

    <input type="submit" value = "生成二維碼">
</form>
<img src="{$img}" alt="">

控制器:IndexController.class.php

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './upload/',
            'savePath'   =>    '',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 實例化上傳類
        // 上傳單個文件
        $info   =   $upload->uploadOne($_FILES['test']);
        if(!$info) {// 上傳錯誤提示錯誤信息
            $qrcode_path_new = './Public/Home/images/code'.'_'.date("Ymdhis").'.png';
            $content = $_POST['content'];
            $matrixPointSize = $_POST['matrixPointSize'];
            $matrixMarginSize = $_POST['matrixMarginSize'];
            $errorCorrectionLevel = $_POST['errorCorrectionLevel'];
            makecode_no_pic($content,$qrcode_path_new,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel);
            $this->assign('img',$qrcode_path_new);
        }else{
            $qrcode_path = "./upload/".$info['savepath'].$info['savename'];
            $content = $_POST['content'];
            $matrixPointSize = $_POST['matrixPointSize'];
            $matrixMarginSize = $_POST['matrixMarginSize'];
            $errorCorrectionLevel = $_POST['errorCorrectionLevel'];
            $url = "./upload/qrcode/".date("YmdHis").".png";
            makecode($qrcode_path,$content,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel,$url);
            $this->assign('img',$url);
            }
        $this->display();
    }
}

然后在項目Application/Home/Common下新建function.php

<?php
function makecode($qrcode_path,$content,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel,$url){
        /**     參數詳情:
         *      $qrcode_path:logo地址
         *      $content:需要生成二維碼的內容
         *      $matrixPointSize:二維碼尺寸大小
         *      $matrixMarginSize:生成二維碼的邊距
         *      $errorCorrectionLevel:容錯級別
         *      $url:生成的帶logo的二維碼地址
         * */
        ob_clean ();
        Vendor('phpqrcode.phpqrcode');
        $object = new \QRcode();
        $qrcode_path_new = './Public/Home/images/code'.'_'.date("Ymdhis").'.png';//定義生成二維碼的路徑及名稱
        $object::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);
        $QR = imagecreatefromstring(file_get_contents($qrcode_path_new));//imagecreatefromstring:創建一個圖像資源從字符串中的圖像流
        $logo = imagecreatefromstring(file_get_contents($qrcode_path));
        $QR_width = imagesx($QR);// 獲取圖像寬度函數
        $QR_height = imagesy($QR);//獲取圖像高度函數
        $logo_width = imagesx($logo);// 獲取圖像寬度函數
        $logo_height = imagesy($logo);//獲取圖像高度函數
        $logo_qr_width = $QR_width / 4;//logo的寬度
        $scale = $logo_width / $logo_qr_width;//計算比例
        $logo_qr_height = $logo_height / $scale;//計算logo高度
        $from_width = ($QR_width - $logo_qr_width) / 2;//規定logo的坐標位置
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        /**     imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
         *      參數詳情:
         *      $dst_image:目標圖象連接資源。
         *      $src_image:源圖象連接資源。
         *      $dst_x:目標 X 坐標點。
         *      $dst_y:目標 Y 坐標點。
         *      $src_x:源的 X 坐標點。
         *      $src_y:源的 Y 坐標點。
         *      $dst_w:目標寬度。
         *      $dst_h:目標高度。
         *      $src_w:源圖象的寬度。
         *      $src_h:源圖象的高度。
         * */
        Header("Content-type: image/png");
        //$url:定義生成帶logo的二維碼的地址及名稱
        imagepng($QR,$url);
    }
    function makecode_no_pic($content,$qrcode_path_new,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel){
        ob_clean ();
        Vendor('phpqrcode.phpqrcode');
        $object = new \QRcode();
        $object::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);
    }

在沒有圖片上傳的情況下,生成我二維碼只是單純的二維碼,只有在有logo上傳的時候,才會生成帶logo的二維碼。

如下圖,即沒有logo時生成的二維碼:

下圖即在有圖片被上傳的時候生成的二維碼:

案例源碼下載:ThinkPHP_3.2.2.zip

pgpqrcode下載phpqrcode.rar

 


免責聲明!

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



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