| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace app\api\service;
- use Endroid\QrCode\ErrorCorrectionLevel;
- use Endroid\QrCode\QrCode as QrCoder;
- use think\facade\App;
- class Qrcode
- {
- 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 = 'writefile'; // display-直接显示 writefile-写入文件
- protected $_file_path = 'uploads/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]; // 背景色
- /**
- * QrcodeServer constructor.
- * @param $config
- */
- 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_path']) && $this->_file_path = $config['file_path'];
- }
- /**
- * 生成二维码
- * @param $content
- * @return array|string
- * @throws \Endroid\QrCode\Exception\InvalidPathException
- */
- public function createServer($content)
- {
- $this->_qr = new Qrcoder($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') {
- // 写入文件
- return $this->generateImg($this->_file_path);
- } else {
- return ['success' => false, 'message' => 'the generate type not found', 'data' => ''];
- }
- }
- /**
- * 生成文件
- *
- * @param $file_path
- * @return array
- */
- public function generateImg($file_path)
- {
- $file_name = uniqid() . '.' . self::WRITE_NAME;
- $save_path = App::getRootPath() . 'public/' . $file_path;
- if (!file_exists($save_path)) {
- mkdir($save_path, 0777, true);
- }
- try {
- $save_file = $save_path . '/' . $file_name;
- $this->_qr->writeFile($save_file);
- // 添加logo
- $logo = 'logo.png';//准备好的logo图片
- $QR = $save_file;//已经生成的原始二维码图
- if ($logo !== FALSE) {
- $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);
- }
- //输出图片
- imagepng($QR, $save_file);
- // 数据返回
- $data = [
- 'url' => $file_path . '/' . $file_name,
- 'ext' => self::WRITE_NAME,
- ];
- return ['success' => true, 'message' => 'write qrimg success', 'data' => $data];
- } catch (\Exception $e) {
- return ['success' => false, 'message' => $e->getMessage(), 'data' => ''];
- }
- }
- }
|