亚洲精品久久国产精品37p,亚洲av无码av制服另类专区,午夜直播免费看,玩弄人妻少妇500系列视频,无码人妻久久久一区二区三区

現(xiàn)在很多框架都支持且僅支持使用composer來安裝和管理,比如最新的thinkphp6,對于很多沒接觸過composer的人很迷茫,但在百度thinkphp6怎么生成二維碼,大多沒結(jié)果。
剛好最近在用thinkphp6重寫一個(gè)項(xiàng)目,遇到生成二維碼這塊,顯然之前的phpqrcode是不能再用了,而且composer上面也沒有phpqrcode的版本,所以呢,用Endroid/QrCode替代,寫好來分享給大家。
composer的介紹和安裝今天先不說,直接進(jìn)入“使用composer安裝和使用endroid/QrCode來生成二維碼”。

1、安裝composer

2、使用composer命令安裝Endroid/QrCode

composer require endroid/qr-code

3、實(shí)現(xiàn)代碼
//生成二維碼的功能無非就是:/二維碼上的內(nèi)容、二維碼的尺寸大小、二維碼上的LOGO、二維碼上的文字….
在項(xiàng)目目錄vendor\endroid\qr-code\src\QrCode.php

<?php

declare(strict_types=1);


namespace Endroid\QrCode;

use BaconQrCode\Encoder\Encoder;
use Endroid\QrCode\Exception\InvalidPathException;
use Endroid\QrCode\Exception\UnsupportedExtensionException;
use Endroid\QrCode\Writer\WriterInterface;

class QrCode implements QrCodeInterface
{
   const LABEL_FONT_PATH_DEFAULT = __DIR__.'/../assets/fonts/noto_sans.otf';

   private $text;

   private $size = 300;
   private $margin = 10;

   private $foregroundColor = [
       'r' => 0,
       'g' => 0,
       'b' => 0,
       'a' => 0,
   ];

   private $backgroundColor = [
       'r' => 255,
       'g' => 255,
       'b' => 255,
       'a' => 0,
   ];

   private $encoding = 'UTF-8';
   private $roundBlockSize = true;
   private $errorCorrectionLevel;

   private $logoPath;
   private $logoWidth;
   private $logoHeight;

   private $label;
   private $labelFontSize = 16;
   private $labelFontPath = self::LABEL_FONT_PATH_DEFAULT;
   private $labelAlignment;
   private $labelMargin = [
       't' => 0,
       'r' => 10,
       'b' => 10,
       'l' => 10,
   ];

   private $writerRegistry;
   private $writer;
   private $writerOptions = [];
   private $validateResult = false;

   public function __construct(string $text = '')
   {
       $this->text = $text;

       $this->errorCorrectionLevel = new ErrorCorrectionLevel(ErrorCorrectionLevel::LOW);
       $this->labelAlignment = new LabelAlignment(LabelAlignment::CENTER);

       $this->createWriterRegistry();
   }

   public function setText(string $text): void
   {
       $this->text = $text;
   }

   public function getText(): string
   {
       return $this->text;
   }

   public function setSize(int $size): void
   {
       $this->size = $size;
   }

   public function getSize(): int
   {
       return $this->size;
   }

   public function setMargin(int $margin): void
   {
       $this->margin = $margin;
   }

   public function getMargin(): int
   {
       return $this->margin;
   }

   public function setForegroundColor(array $foregroundColor): void
   {
       if (!isset($foregroundColor['a'])) {
           $foregroundColor['a'] = 0;
       }

       foreach ($foregroundColor as &$color) {
           $color = intval($color);
       }

       $this->foregroundColor = $foregroundColor;
   }

   public function getForegroundColor(): array
   {
       return $this->foregroundColor;
   }

   public function setBackgroundColor(array $backgroundColor): void
   {
       if (!isset($backgroundColor['a'])) {
           $backgroundColor['a'] = 0;
       }

       foreach ($backgroundColor as &$color) {
           $color = intval($color);
       }

       $this->backgroundColor = $backgroundColor;
   }

   public function getBackgroundColor(): array
   {
       return $this->backgroundColor;
   }

   public function setEncoding(string $encoding): void
   {
       $this->encoding = $encoding;
   }

   public function getEncoding(): string
   {
       return $this->encoding;
   }

   public function setRoundBlockSize(bool $roundBlockSize): void
   {
       $this->roundBlockSize = $roundBlockSize;
   }

   public function getRoundBlockSize(): bool
   {
       return $this->roundBlockSize;
   }

   public function setErrorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): void
   {
       $this->errorCorrectionLevel = $errorCorrectionLevel;
   }

   public function getErrorCorrectionLevel(): ErrorCorrectionLevel
   {
       return $this->errorCorrectionLevel;
   }

   public function setLogoPath(string $logoPath): void
   {
       $logoPath = realpath($logoPath);

       if (!is_file($logoPath)) {
           throw new InvalidPathException('Invalid logo path: '.$logoPath);
       }

       $this->logoPath = $logoPath;
   }

   public function getLogoPath(): ?string
   {
       return $this->logoPath;
   }

   public function setLogoSize(int $logoWidth, int $logoHeight = null): void
   {
       $this->logoWidth = $logoWidth;
       $this->logoHeight = $logoHeight;
   }

   public function setLogoWidth(int $logoWidth): void
   {
       $this->logoWidth = $logoWidth;
   }

   public function getLogoWidth(): ?int
   {
       return $this->logoWidth;
   }

   public function setLogoHeight(int $logoHeight): void
   {
       $this->logoHeight = $logoHeight;
   }

   public function getLogoHeight(): ?int
   {
       return $this->logoHeight;
   }

   public function setLabel(string $label, int $labelFontSize = null, string $labelFontPath = null, string $labelAlignment = null, array $labelMargin = null): void
   {
       $this->label = $label;

       if (null !== $labelFontSize) {
           $this->setLabelFontSize($labelFontSize);
       }

       if (null !== $labelFontPath) {
           $this->setLabelFontPath($labelFontPath);
       }

       if (null !== $labelAlignment) {
           $this->setLabelAlignment($labelAlignment);
       }

       if (null !== $labelMargin) {
           $this->setLabelMargin($labelMargin);
       }
   }

   public function getLabel(): ?string
   {
       return $this->label;
   }

   public function setLabelFontSize(int $labelFontSize): void
   {
       $this->labelFontSize = $labelFontSize;
   }

   public function getLabelFontSize(): ?int
   {
       return $this->labelFontSize;
   }

   public function setLabelFontPath(string $labelFontPath): void
   {
       $resolvedLabelFontPath = realpath($labelFontPath);

       if (!is_string($resolvedLabelFontPath) || !is_file($resolvedLabelFontPath)) {
           throw new InvalidPathException('Invalid label font path: '.$labelFontPath);
       }

       $this->labelFontPath = $resolvedLabelFontPath;
   }

   public function getLabelFontPath(): ?string
   {
       return $this->labelFontPath;
   }

   public function setLabelAlignment(string $labelAlignment): void
   {
       $this->labelAlignment = new LabelAlignment($labelAlignment);
   }

   public function getLabelAlignment(): ?string
   {
       return $this->labelAlignment->getValue();
   }

   public function setLabelMargin(array $labelMargin): void
   {
       $this->labelMargin = array_merge($this->labelMargin, $labelMargin);
   }

   public function getLabelMargin(): ?array
   {
       return $this->labelMargin;
   }

   public function setWriterRegistry(WriterRegistryInterface $writerRegistry): void
   {
       $this->writerRegistry = $writerRegistry;
   }

   public function setWriter(WriterInterface $writer): void
   {
       $this->writer = $writer;
   }

   public function getWriter(string $name = null): WriterInterface
   {
       if (!is_null($name)) {
           return $this->writerRegistry->getWriter($name);
       }

       if ($this->writer instanceof WriterInterface) {
           return $this->writer;
       }

       return $this->writerRegistry->getDefaultWriter();
   }

   public function setWriterOptions(array $writerOptions): void
   {
       $this->writerOptions = $writerOptions;
   }

   public function getWriterOptions(): array
   {
       return $this->writerOptions;
   }

   private function createWriterRegistry()
   {
       $this->writerRegistry = new WriterRegistry();
       $this->writerRegistry->loadDefaultWriters();
   }

   public function setWriterByName(string $name)
   {
       $this->writer = $this->getWriter($name);
   }

   public function setWriterByPath(string $path): void
   {
       $extension = pathinfo($path, PATHINFO_EXTENSION);

       $this->setWriterByExtension($extension);
   }

   public function setWriterByExtension(string $extension): void
   {
       foreach ($this->writerRegistry->getWriters() as $writer) {
           if ($writer->supportsExtension($extension)) {
               $this->writer = $writer;

               return;
           }
       }

       throw new UnsupportedExtensionException('Missing writer for extension "'.$extension.'"');
   }

   public function writeString(): string
   {
       return $this->getWriter()->writeString($this);
   }

   public function writeDataUri(): string
   {
       return $this->getWriter()->writeDataUri($this);
   }

   public function writeFile(string $path): void
   {
       $this->getWriter()->writeFile($this, $path);
   }

   public function getContentType(): string
   {
       return $this->getWriter()->getContentType();
   }

   public function setValidateResult(bool $validateResult): void
   {
       $this->validateResult = $validateResult;
   }

   public function getValidateResult(): bool
   {
       return $this->validateResult;
   }

   public function getData(): array
   {
       $baconErrorCorrectionLevel = $this->errorCorrectionLevel->toBaconErrorCorrectionLevel();

       $baconQrCode = Encoder::encode($this->text, $baconErrorCorrectionLevel, $this->encoding);

       $matrix = $baconQrCode->getMatrix()->getArray()->toArray();

       foreach ($matrix as &$row) {
           $row = $row->toArray();
       }

       $data = ['matrix' => $matrix];
       $data['block_count'] = count($matrix[0]);
       $data['block_size'] = $this->size / $data['block_count'];
       if ($this->roundBlockSize) {
           $data['block_size'] = intval(floor($data['block_size']));
       }
       $data['inner_width'] = $data['block_size'] * $data['block_count'];
       $data['inner_height'] = $data['block_size'] * $data['block_count'];
       $data['outer_width'] = $this->size + 2 * $this->margin;
       $data['outer_height'] = $this->size + 2 * $this->margin;
       $data['margin_left'] = ($data['outer_width'] - $data['inner_width']) / 2;
       if ($this->roundBlockSize) {
           $data['margin_left'] = intval(floor($data['margin_left']));
       }
       $data['margin_right'] = $data['outer_width'] - $data['inner_width'] - $data['margin_left'];

       return $data;
   }
}

<?php
//引入composer自動(dòng)生成的類加載器
require_once 'vendor/autoload.php';
//命名空間方式調(diào)用QrCode類
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Response\QrCodeResponse;
$qrCode = new QrCode();
$qrCode->setText('設(shè)置二維碼上的內(nèi)容'); //設(shè)置二維碼上的內(nèi)容
$qrCode->setSize(300); //二維碼尺寸
$qrCode->setWriterByName('png'); //設(shè)置輸出的二維碼圖片格式
$qrCode->setMargin(10);
$qrCode->setEncoding('UTF-8');
$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH()); //設(shè)置二維碼的糾錯(cuò)率,可以有l(wèi)ow、medium、quartile、hign多個(gè)糾錯(cuò)率
$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]); //設(shè)置二維碼的rgb顏色和透明度a,這里是黑色
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]); //設(shè)置二維碼圖片的背景底色,這里是白色
//可能的指定二維碼下方的文字,寫死15px的字體大小,字體
$qrCode->setLabelFontPath(__DIR__.'/vendor/endroid/qr-code/assets/fonts/noto_sans.otf'); //字體路徑
$qrCode->setLabel('二維碼下方的文字1'); //文字
$qrCode->setLabelFontSize(16); //字體大小
//或統(tǒng)一用setLabel('二維碼下方的文字','字體大小','字體路徑','對齊方式')來設(shè)置
$qrCode->setLabel('二維碼下方的文字2', 16, __DIR__.'/vendor/endroid/qr-code/assets/fonts/noto_sans.otf', LabelAlignment::CENTER());
$qrCode->setLabelMargin(['t'=>50]); //設(shè)置標(biāo)簽邊距 array('t' => 10,'r' => 20,'b' => 10,'l' => 30)
//如果要加上logo水印,則在調(diào)用setLogoPath和setLogoSize方法
$qrCode->setLogoPath(__DIR__.'/logo.png'); //logo水印圖片的所在的路徑
$qrCode->setLogoSize(150, 200); //設(shè)置logo水印的大小,單位px ,參數(shù)如果是一個(gè)int數(shù)字等比例縮放
//保存二維碼
$qrCode->writeFile(__DIR__.'/qrcode.png');
//輸出二維碼
header('Content-Type: '.$qrCode->getContentType());
exit($qrCode->writeString());
?>


4、注意
QrCode生成中文漢字的label的時(shí)需要引入中文字體,所以需要調(diào)用setLabelFontPath方法傳入一個(gè)中文字體的路徑,QrCode默認(rèn)提供的字體在\vendor\endroid\qrcode\assets\font路徑下,但QrCode類并未默認(rèn)調(diào)用,另外需要使用UTF8編碼的中文設(shè)置label。

5、各參數(shù)詳解
參數(shù)名 描述 示例
setText 設(shè)置文本 
http://www.muniuliuma.cn
setSize 設(shè)置二維碼的大小,這里二維碼應(yīng)該是正方形的,所以相當(dāng)于長寬 400
setMargin 設(shè)置二維碼邊距 10
setForegroundColor 設(shè)置前景色,RGB顏色 array(‘r’ => 0, ‘g’ => 0, ‘b’ => 0, ‘a(chǎn)’ => 0)
setBackgroundColor 設(shè)置背景色,RGB顏色 array(‘r’ => 0, ‘g’ => 0, ‘b’ => 0, ‘a(chǎn)’ => 0)
setEncoding 設(shè)置編碼 utf8
setErrorCorrectionLevel 設(shè)置錯(cuò)誤級別(low / medium / quartile / high) high
setLogoPath 設(shè)置logo路徑 logo.png
setLogoWidth 設(shè)置logo大小 50
setLabel 設(shè)置標(biāo)簽 test
setLabelFontSize 設(shè)置標(biāo)簽字體大小 16
setLabelFontPath 設(shè)置標(biāo)簽字體路徑 null
setLabelAlignment 設(shè)置標(biāo)簽對齊方式(left / center / right) center
setLabelMargin 設(shè)置標(biāo)簽邊距 array(‘t’ => 10,’r’ => 20,’b’ => 10,’l’ => 30)
setWriterRegistry
setWriter
setWriterByName 寫入文件的后綴名 png
setWriterByPath
setWriterByExtension
setValidateResult
writeString
writeDataUri
writeFile 寫入文件 test.png
6、方法調(diào)用
$this->QrCode($data);//方法調(diào)用

/**
* 生成二維碼方法
*/
public function QrCode($order_number)
{
   $content = 'http://fanxin.me:8080?order_number=' . $order_number;
   $url = root_path() . '/public/upload_resource/qrcode/' . $order_number . '.png';//存框架的位置
   $code_url = 'http://fanxin.me:8080/upload_resource/qrcode/' . $order_number . '.png';//二維碼的訪問地址
   $qrCode=new QrCode($content);
   // 指定內(nèi)容類型
   header('Content-Type: ' . $qrCode->getContentType());
   // 輸出二維碼
   $qrCode->writeFile($url);
   return $code_url;
}


評論(0條)

刀客源碼 游客評論