ThinkPHP3.2.3擴張之二維碼

來源:互聯網
上載者:User
ThinkPHP3.2.3擴充之二維碼

先安照路徑放好。

簡單使用無logo:

public function qrcode(){        Vendor('phpqrcode.phpqrcode');        //產生二維碼圖片        $object = new \QRcode();        $url='http://www.shouce.ren/';//網址或者是常值內容        $level=3;        $size=4;        $errorCorrectionLevel =intval($level) ;//容錯層級        $matrixPointSize = intval($size);//產生圖片大小        $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);    }

進階使用帶logo:

public function qrcode(){        Vendor('phpqrcode.phpqrcode');        //產生二維碼圖片        $object = new \QRcode();        $qrcode_path='';        $file_tmp_name='';        $errors=array();        if(!empty($_POST)){            $content = trim($_POST['content']); //二維碼內容            $contentSize=$this->getStringLength($content);            if($contentSize>150){                $errors[]='字數過長,不能多於150個字元!';            }            if(isset($_FILES['upimage']['tmp_name']) && $_FILES['upimage']['tmp_name'] && is_uploaded_file($_FILES['upimage']['tmp_name'])){                if($_FILES['upimage']['size']>512000){                    $errors[]="你上傳的檔案過大,最大不能超過500K。";                }                $file_tmp_name=$_FILES['upimage']['tmp_name'];                $fileext = array("image/pjpeg","image/jpeg","image/gif","image/x-png","image/png");                if(!in_array($_FILES['upimage']['type'],$fileext)){                    $errors[]="你上傳的檔案格式不正確,僅支援 png, jpg, gif格式。";                }            }            $tpgs=$_POST['tpgs'];//圖片格式            $qrcode_bas_path='upload/qrcode/';            if(!is_dir($qrcode_bas_path)){                mkdir($qrcode_bas_path, 0777, true);            }            $uniqid_rand=date("Ymdhis").uniqid(). rand(1,1000);            $qrcode_path=$qrcode_bas_path.$uniqid_rand. "_1.".$tpgs;//原始圖片路徑            $qrcode_path_new=$qrcode_bas_path.$uniqid_rand."_2.".$tpgs;//二維碼圖片路徑            if(Helper::getOS()=='Linux'){                $mv = move_uploaded_file($file_tmp_name, $qrcode_path);            }else{                //解決windows下中文檔案名稱亂碼的問題                $save_path = Helper::safeEncoding($qrcode_path,'GB2312');                if(!$save_path){                    $errors[]='上傳失敗,請重試!';                }                $mv = move_uploaded_file($file_tmp_name, $qrcode_path);            }            if(empty($errors)){                $errorCorrectionLevel = $_POST['errorCorrectionLevel'];//容錯層級                $matrixPointSize = $_POST['matrixPointSize'];//產生圖片大小                $matrixMarginSize = $_POST['matrixMarginSize'];//邊距大小                //產生二維碼圖片                $object::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);                $QR = $qrcode_path_new;//已經產生的原始二維碼圖                $logo = $qrcode_path;//準備好的logo圖片                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 / 5;                    $scale = $logo_width/$logo_qr_width;                    $logo_qr_height = $logo_height/$scale;                    $from_width = ($QR_width - $logo_qr_width) / 2;                    //重新組合圖片並調整大小                    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,                    $logo_qr_height, $logo_width, $logo_height);                    //輸出圖片                    //header("Content-type: image/png");                    imagepng($QR,$qrcode_path);                    imagedestroy($QR);                }else{                    $qrcode_path=$qrcode_path_new;                }            }else{                $qrcode_path='';            }        }        $data=array('data'=>array('errors'=>$errors,'qrcode_path'=>$qrcode_path));        $this->assign('data',$data);        $this->display();

示範地址:http://www.shouce.ren/tool/qrcode

用到助手類Helper地址:http://www.thinkphp.cn/topic/34875.html

  

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.