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
來源:簡書
?
總結
- 上一篇: 【原版教材•中英对照】高分子化学—可作为
- 下一篇: COMSOL有限元仿真深度指南:连接薄结