日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tp5 二维码生成

發布時間:2024/3/24 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tp5 二维码生成 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 使用 composer 安裝 endroid/qrcode:

composer require endroid/qrcode

2.將二維碼生成封裝為服務

?

image.png


QrcodeServer.php代碼如下:

?

<?php /*** Created by PhpStorm.* User: cdjyj21* Date: 2018/9/4* Time: 11:57*/namespace app\services;use Endroid\QrCode\ErrorCorrectionLevel; use Endroid\QrCode\LabelAlignment; use Endroid\QrCode\QrCode;class QrcodeServer {protected $_qr;protected $_encoding = 'UTF-8'; // 編碼類型protected $_size = 300; // 二維碼大小protected $_logo = false; // 是否需要帶logo的二維碼protected $_logo_url = ''; // logo圖片路徑protected $_logo_size = 80; // logo大小protected $_title = false; // 是否需要二維碼titleprotected $_title_content = ''; // title內容protected $_generate = 'display'; // display-直接顯示 writefile-寫入文件protected $_file_name = './static/qrcode'; // 寫入文件路徑const MARGIN = 10; // 二維碼內容相對于整張圖片的外邊距const WRITE_NAME = 'png'; // 寫入文件的后綴名const FOREGROUND_COLOR = ['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]; // 前景色const BACKGROUND_COLOR = ['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]; // 背景色public function __construct($config) {isset($config['generate']) && $this->_generate = $config['generate'];isset($config['encoding']) && $this->_encoding = $config['encoding'];isset($config['size']) && $this->_size = $config['size'];isset($config['logo']) && $this->_logo = $config['logo'];isset($config['logo_url']) && $this->_logo_url = $config['logo_url'];isset($config['logo_size']) && $this->_logo_size = $config['logo_size'];isset($config['title']) && $this->_title = $config['title'];isset($config['title_content']) && $this->_title_content = $config['title_content'];isset($config['file_name']) && $this->_file_name = $config['file_name'];}/*** 生成二維碼* @param $content //需要寫入的內容* @return array | page input*/public function createServer($content) {$this->_qr = new QrCode($content);$this->_qr->setSize($this->_size);$this->_qr->setWriterByName(self::WRITE_NAME);$this->_qr->setMargin(self::MARGIN);$this->_qr->setEncoding($this->_encoding);$this->_qr->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH); // 容錯率$this->_qr->setForegroundColor(self::FOREGROUND_COLOR);$this->_qr->setBackgroundColor(self::BACKGROUND_COLOR);// 是否需要titleif ($this->_title) {$this->_qr->setLabel($this->_title_content, 16, null, LabelAlignment::CENTER);}// 是否需要logoif ($this->_logo) {$this->_qr->setLogoPath($this->_logo_url);$this->_qr->setLogoWidth($this->_logo_size);}$this->_qr->setValidateResult(false);if ($this->_generate == 'display') {// 展示二維碼// 前端調用 例:<img src="http://localhost/qr.php?url=base64_url_string">header('Content-Type: ' . $this->_qr->getContentType());return $this->_qr->writeString();} else if ($this->_generate == 'writefile') {// 寫入文件$file_name = $this->_file_name;return $this->generateImg($file_name);} else {return ['success' => false, 'message' => 'the generate type not found', 'data' => ''];}}/*** 生成文件* @param $file_name //目錄文件 例: /tmp* @return array*/public function generateImg($file_name) {$file_path = $file_name . DIRECTORY_SEPARATOR . uniqid() . '.' . self::WRITE_NAME;if (!file_exists($file_name)) {mkdir($file_name, 0777, true);}try {$this->_qr->writeFile($file_path);$data = ['url' => $file_path,'ext' => self::WRITE_NAME,];return ['success' => true, 'message' => 'write qrimg success', 'data' => $data];} catch (\Exception $e) {return ['success' => false, 'message' => $e->getMessage(), 'data' => ''];}}}

3.調用

例:

<?php /*** Created by PhpStorm.* User: cdjyj21* Date: 2018/9/4* Time: 11:57*/namespace app\test\controller;use app\services\QrcodeServer;class Qrcode {/*** 直接輸出二維碼 + 生成二維碼圖片文件*/public function create(){// 自定義二維碼配置$config = ['title' => true,'title_content' => 'test','logo' => true,'logo_url' => './logo.png','logo_size' => 80,];// 直接輸出$qr_url = 'http://www.baidu.com?id=' . rand(1000, 9999);$qr_code = new QrcodeServer($config);$qr_img = $qr_code->createServer($qr_url);echo $qr_img;// 寫入文件$qr_url = '這是個測試二維碼';$file_name = './static/qrcode'; // 定義保存目錄$config['file_name'] = $file_name;$config['generate'] = 'writefile';$qr_code = new QrcodeServer($config);$rs = $qr_code->createServer($qr_url);print_r($rs);exit;} }

在瀏覽器中直接訪問create()方法,會直接輸出二維碼,同時會在自定義保存目錄下生成一張二維碼圖片。效果如下:


那這種直接輸出的二維碼怎么應用于項目中呢,一般都是直接寫在html 中的 <img> 標簽中,例如:

?

<img src="http://localhost:8080/projecttest/qrtest?id=1234" alt="這是一個二維碼" />

這里羅列下我看懂的幾個參數,也算給自己做個筆記吧。

參數名描述示例
setText設置文本https://www.baidu.com
setSize設置二維碼的大小,這里二維碼應該是正方形的,所以相當于長寬400
setMargin設置二維碼邊距10
setForegroundColor設置前景色,RGB顏色array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0)
setBackgroundColor設置背景色,RGB顏色array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0)
setEncoding設置編碼utf8
setErrorCorrectionLevel設置錯誤級別(low / medium / quartile / high)high
setLogoPath設置logo路徑logo.png
setLogoWidth設置logo大小50
setLabel設置標簽test
setLabelFontSize設置標簽字體大小16
setLabelFontPath設置標簽字體路徑null
setLabelAlignment設置標簽對齊方式(left / center / right)center
setLabelMargin設置標簽邊距array('t' => 10,'r' => 20,'b' => 10,'l' => 30)
setWriterRegistry??
setWriter??
setWriterByName寫入文件的后綴名png
setWriterByPath??
setWriterByExtension??
setValidateResult??
writeString??
writeDataUri??
writeFile寫入文件test.png



作者:紅塵一落君莫笑
鏈接:https://www.jianshu.com/p/9b933907acd6
來源:簡書
?

總結

以上是生活随笔為你收集整理的tp5 二维码生成的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。