截流自动化的商城平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * (c) Jeroen van den Enden <info@endroid.nl>
  5. *
  6. * This source file is subject to the MIT license that is bundled
  7. * with this source code in the file LICENSE.
  8. */
  9. namespace Endroid\QrCode;
  10. use BaconQrCode\Encoder\Encoder;
  11. use Endroid\QrCode\Exception\InvalidFontException;
  12. use Endroid\QrCode\Exception\UnsupportedExtensionException;
  13. use Endroid\QrCode\Exception\ValidationException;
  14. use Endroid\QrCode\Writer\WriterInterface;
  15. class QrCode implements QrCodeInterface
  16. {
  17. const LABEL_FONT_PATH_DEFAULT = __DIR__.'/../assets/fonts/noto_sans.otf';
  18. const ROUND_BLOCK_SIZE_MODE_MARGIN = 'margin';
  19. const ROUND_BLOCK_SIZE_MODE_SHRINK = 'shrink';
  20. const ROUND_BLOCK_SIZE_MODE_ENLARGE = 'enlarge';
  21. private $text;
  22. /** @var int */
  23. private $size = 300;
  24. /** @var int */
  25. private $margin = 10;
  26. /** @var array */
  27. private $foregroundColor = [
  28. 'r' => 0,
  29. 'g' => 0,
  30. 'b' => 0,
  31. 'a' => 0,
  32. ];
  33. /** @var array */
  34. private $backgroundColor = [
  35. 'r' => 255,
  36. 'g' => 255,
  37. 'b' => 255,
  38. 'a' => 0,
  39. ];
  40. /** @var string */
  41. private $encoding = 'UTF-8';
  42. /** @var bool */
  43. private $roundBlockSize = true;
  44. /** @var string */
  45. private $roundBlockSizeMode = self::ROUND_BLOCK_SIZE_MODE_MARGIN;
  46. private $errorCorrectionLevel;
  47. /** @var string */
  48. private $logoPath;
  49. /** @var int|null */
  50. private $logoWidth;
  51. /** @var int|null */
  52. private $logoHeight;
  53. /** @var string */
  54. private $label;
  55. /** @var int */
  56. private $labelFontSize = 16;
  57. /** @var string */
  58. private $labelFontPath = self::LABEL_FONT_PATH_DEFAULT;
  59. private $labelAlignment;
  60. /** @var array */
  61. private $labelMargin = [
  62. 't' => 0,
  63. 'r' => 10,
  64. 'b' => 10,
  65. 'l' => 10,
  66. ];
  67. /** @var WriterRegistryInterface */
  68. private $writerRegistry;
  69. /** @var WriterInterface|null */
  70. private $writer;
  71. /** @var array */
  72. private $writerOptions = [];
  73. /** @var bool */
  74. private $validateResult = false;
  75. public function __construct(string $text = '')
  76. {
  77. $this->text = $text;
  78. $this->errorCorrectionLevel = ErrorCorrectionLevel::LOW();
  79. $this->labelAlignment = LabelAlignment::CENTER();
  80. $this->createWriterRegistry();
  81. }
  82. public function setText(string $text): void
  83. {
  84. $this->text = $text;
  85. }
  86. public function getText(): string
  87. {
  88. return $this->text;
  89. }
  90. public function setSize(int $size): void
  91. {
  92. $this->size = $size;
  93. }
  94. public function getSize(): int
  95. {
  96. return $this->size;
  97. }
  98. public function setMargin(int $margin): void
  99. {
  100. $this->margin = $margin;
  101. }
  102. public function getMargin(): int
  103. {
  104. return $this->margin;
  105. }
  106. public function setForegroundColor(array $foregroundColor): void
  107. {
  108. if (!isset($foregroundColor['a'])) {
  109. $foregroundColor['a'] = 0;
  110. }
  111. foreach ($foregroundColor as &$color) {
  112. $color = intval($color);
  113. }
  114. $this->foregroundColor = $foregroundColor;
  115. }
  116. public function getForegroundColor(): array
  117. {
  118. return $this->foregroundColor;
  119. }
  120. public function setBackgroundColor(array $backgroundColor): void
  121. {
  122. if (!isset($backgroundColor['a'])) {
  123. $backgroundColor['a'] = 0;
  124. }
  125. foreach ($backgroundColor as &$color) {
  126. $color = intval($color);
  127. }
  128. $this->backgroundColor = $backgroundColor;
  129. }
  130. public function getBackgroundColor(): array
  131. {
  132. return $this->backgroundColor;
  133. }
  134. public function setEncoding(string $encoding): void
  135. {
  136. $this->encoding = $encoding;
  137. }
  138. public function getEncoding(): string
  139. {
  140. return $this->encoding;
  141. }
  142. public function setRoundBlockSize(bool $roundBlockSize, string $roundBlockSizeMode = self::ROUND_BLOCK_SIZE_MODE_MARGIN): void
  143. {
  144. $this->roundBlockSize = $roundBlockSize;
  145. $this->setRoundBlockSizeMode($roundBlockSizeMode);
  146. }
  147. public function getRoundBlockSize(): bool
  148. {
  149. return $this->roundBlockSize;
  150. }
  151. public function setRoundBlockSizeMode(string $roundBlockSizeMode): void
  152. {
  153. if (!in_array($roundBlockSizeMode, [
  154. self::ROUND_BLOCK_SIZE_MODE_ENLARGE,
  155. self::ROUND_BLOCK_SIZE_MODE_MARGIN,
  156. self::ROUND_BLOCK_SIZE_MODE_SHRINK,
  157. ])) {
  158. throw new ValidationException('Invalid round block size mode: '.$roundBlockSizeMode);
  159. }
  160. $this->roundBlockSizeMode = $roundBlockSizeMode;
  161. }
  162. public function setErrorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): void
  163. {
  164. $this->errorCorrectionLevel = $errorCorrectionLevel;
  165. }
  166. public function getErrorCorrectionLevel(): ErrorCorrectionLevel
  167. {
  168. return $this->errorCorrectionLevel;
  169. }
  170. public function setLogoPath(string $logoPath): void
  171. {
  172. $this->logoPath = $logoPath;
  173. }
  174. public function getLogoPath(): ?string
  175. {
  176. return $this->logoPath;
  177. }
  178. public function setLogoSize(int $logoWidth, int $logoHeight = null): void
  179. {
  180. $this->logoWidth = $logoWidth;
  181. $this->logoHeight = $logoHeight;
  182. }
  183. public function setLogoWidth(int $logoWidth): void
  184. {
  185. $this->logoWidth = $logoWidth;
  186. }
  187. public function getLogoWidth(): ?int
  188. {
  189. return $this->logoWidth;
  190. }
  191. public function setLogoHeight(int $logoHeight): void
  192. {
  193. $this->logoHeight = $logoHeight;
  194. }
  195. public function getLogoHeight(): ?int
  196. {
  197. return $this->logoHeight;
  198. }
  199. public function setLabel(string $label, int $labelFontSize = null, string $labelFontPath = null, string $labelAlignment = null, array $labelMargin = null): void
  200. {
  201. $this->label = $label;
  202. if (null !== $labelFontSize) {
  203. $this->setLabelFontSize($labelFontSize);
  204. }
  205. if (null !== $labelFontPath) {
  206. $this->setLabelFontPath($labelFontPath);
  207. }
  208. if (null !== $labelAlignment) {
  209. $this->setLabelAlignment($labelAlignment);
  210. }
  211. if (null !== $labelMargin) {
  212. $this->setLabelMargin($labelMargin);
  213. }
  214. }
  215. public function getLabel(): ?string
  216. {
  217. return $this->label;
  218. }
  219. public function setLabelFontSize(int $labelFontSize): void
  220. {
  221. $this->labelFontSize = $labelFontSize;
  222. }
  223. public function getLabelFontSize(): int
  224. {
  225. return $this->labelFontSize;
  226. }
  227. public function setLabelFontPath(string $labelFontPath): void
  228. {
  229. $resolvedLabelFontPath = (string) realpath($labelFontPath);
  230. if (!is_file($resolvedLabelFontPath)) {
  231. throw new InvalidFontException('Invalid label font path: '.$labelFontPath);
  232. }
  233. $this->labelFontPath = $resolvedLabelFontPath;
  234. }
  235. public function getLabelFontPath(): string
  236. {
  237. return $this->labelFontPath;
  238. }
  239. public function setLabelAlignment(string $labelAlignment): void
  240. {
  241. $this->labelAlignment = new LabelAlignment($labelAlignment);
  242. }
  243. public function getLabelAlignment(): string
  244. {
  245. return $this->labelAlignment->getValue();
  246. }
  247. public function setLabelMargin(array $labelMargin): void
  248. {
  249. $this->labelMargin = array_merge($this->labelMargin, $labelMargin);
  250. }
  251. public function getLabelMargin(): array
  252. {
  253. return $this->labelMargin;
  254. }
  255. public function setWriterRegistry(WriterRegistryInterface $writerRegistry): void
  256. {
  257. $this->writerRegistry = $writerRegistry;
  258. }
  259. public function setWriter(WriterInterface $writer): void
  260. {
  261. $this->writer = $writer;
  262. }
  263. public function getWriter(string $name = null): WriterInterface
  264. {
  265. if (!is_null($name)) {
  266. return $this->writerRegistry->getWriter($name);
  267. }
  268. if ($this->writer instanceof WriterInterface) {
  269. return $this->writer;
  270. }
  271. return $this->writerRegistry->getDefaultWriter();
  272. }
  273. public function setWriterOptions(array $writerOptions): void
  274. {
  275. $this->writerOptions = $writerOptions;
  276. }
  277. public function getWriterOptions(): array
  278. {
  279. return $this->writerOptions;
  280. }
  281. private function createWriterRegistry(): void
  282. {
  283. $this->writerRegistry = new WriterRegistry();
  284. $this->writerRegistry->loadDefaultWriters();
  285. }
  286. public function setWriterByName(string $name): void
  287. {
  288. $this->writer = $this->getWriter($name);
  289. }
  290. public function setWriterByPath(string $path): void
  291. {
  292. $extension = pathinfo($path, PATHINFO_EXTENSION);
  293. $this->setWriterByExtension($extension);
  294. }
  295. public function setWriterByExtension(string $extension): void
  296. {
  297. foreach ($this->writerRegistry->getWriters() as $writer) {
  298. if ($writer->supportsExtension($extension)) {
  299. $this->writer = $writer;
  300. return;
  301. }
  302. }
  303. throw new UnsupportedExtensionException('Missing writer for extension "'.$extension.'"');
  304. }
  305. public function writeString(): string
  306. {
  307. return $this->getWriter()->writeString($this);
  308. }
  309. public function writeDataUri(): string
  310. {
  311. return $this->getWriter()->writeDataUri($this);
  312. }
  313. public function writeFile(string $path): void
  314. {
  315. $this->getWriter()->writeFile($this, $path);
  316. }
  317. public function getContentType(): string
  318. {
  319. return $this->getWriter()->getContentType();
  320. }
  321. public function setValidateResult(bool $validateResult): void
  322. {
  323. $this->validateResult = $validateResult;
  324. }
  325. public function getValidateResult(): bool
  326. {
  327. return $this->validateResult;
  328. }
  329. public function getData(): array
  330. {
  331. $baconErrorCorrectionLevel = $this->errorCorrectionLevel->toBaconErrorCorrectionLevel();
  332. $baconQrCode = Encoder::encode($this->text, $baconErrorCorrectionLevel, $this->encoding);
  333. $baconMatrix = $baconQrCode->getMatrix();
  334. $matrix = [];
  335. $columnCount = $baconMatrix->getWidth();
  336. $rowCount = $baconMatrix->getHeight();
  337. for ($rowIndex = 0; $rowIndex < $rowCount; ++$rowIndex) {
  338. $matrix[$rowIndex] = [];
  339. for ($columnIndex = 0; $columnIndex < $columnCount; ++$columnIndex) {
  340. $matrix[$rowIndex][$columnIndex] = $baconMatrix->get($columnIndex, $rowIndex);
  341. }
  342. }
  343. $data = ['matrix' => $matrix];
  344. $data['block_count'] = count($matrix[0]);
  345. $data['block_size'] = $this->size / $data['block_count'];
  346. if ($this->roundBlockSize) {
  347. switch ($this->roundBlockSizeMode) {
  348. case self::ROUND_BLOCK_SIZE_MODE_ENLARGE:
  349. $data['block_size'] = intval(ceil($data['block_size']));
  350. $this->size = $data['block_size'] * $data['block_count'];
  351. break;
  352. case self::ROUND_BLOCK_SIZE_MODE_SHRINK:
  353. $data['block_size'] = intval(floor($data['block_size']));
  354. $this->size = $data['block_size'] * $data['block_count'];
  355. break;
  356. case self::ROUND_BLOCK_SIZE_MODE_MARGIN:
  357. default:
  358. $data['block_size'] = intval(floor($data['block_size']));
  359. }
  360. }
  361. $data['inner_width'] = $data['block_size'] * $data['block_count'];
  362. $data['inner_height'] = $data['block_size'] * $data['block_count'];
  363. $data['outer_width'] = $this->size + 2 * $this->margin;
  364. $data['outer_height'] = $this->size + 2 * $this->margin;
  365. $data['margin_left'] = ($data['outer_width'] - $data['inner_width']) / 2;
  366. if ($this->roundBlockSize) {
  367. $data['margin_left'] = intval(floor($data['margin_left']));
  368. }
  369. $data['margin_right'] = $data['outer_width'] - $data['inner_width'] - $data['margin_left'];
  370. return $data;
  371. }
  372. }