QrCode.php 11 KB

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