composer 連接: https://packagist.org/packages/endroid/qrcodephp
注意:PHP版本 要求 7.1+前端
1. 使用 composer 安裝 endroid/qrcode:app
composer require endroid/qrcode
2 將二維碼生成封裝爲服務composer
位置: /appliction/common/services/QrcodeService.php測試
3 QrcodeServer.php 代碼以下ui
<?php /** * 二維碼服務 * 依然範兒特西 */ namespace app\common\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; // 是否須要二維碼title protected $_title_content = ''; // title內容 protected $_generate = 'display'; // display-直接顯示 writefile-寫入文件 protected $_file_name = './'; // 寫入文件路徑 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(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH)); // 容錯率 $this->_qr->setForegroundColor(self::FOREGROUND_COLOR); $this->_qr->setBackgroundColor(self::BACKGROUND_COLOR); // 是否須要title if ($this->_title) { $this->_qr->setLabel($this->_title_content, 16, null, LabelAlignment::CENTER); } // 是否須要logo if ($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' => '']; } } }
4 調用this
<?php /** * 二維碼 */ namespace app\index\controller; use app\common\services\QrcodeServer;
class Qrcode { /** * 直接輸出二維碼 + 生成二維碼圖片文件 */ public function create(){ // 自定義二維碼配置 $config = [ 'title' => true, 'title_content' => '嗨,老範', '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; } }
效果: 編碼