Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Output/QRFpdf.php

raw
1 <?php
2 /**
3 * Class QRFpdf
4 *
5 * @created 03.06.2020
6 * @author Maximilian Kresse
7 * @license MIT
8 *
9 * @see https://github.com/chillerlan/php-qrcode/pull/49
10 */
11 declare(strict_types=1);
12
13 namespace chillerlan\QRCode\Output;
14
15 use chillerlan\QRCode\QROptions;
16 use chillerlan\QRCode\Data\QRMatrix;
17 use chillerlan\Settings\SettingsContainerInterface;
18 use FPDF;
19
20 use function class_exists;
21
22 /**
23 * QRFpdf output module (requires fpdf)
24 *
25 * @see https://github.com/Setasign/FPDF
26 * @see http://www.fpdf.org/
27 */
28 class QRFpdf extends QROutputAbstract{
29 use RGBArrayModuleValueTrait;
30
31 final public const MIME_TYPE = 'application/pdf';
32
33 /** @var int[]|null */
34 protected array|null $prevColor = null;
35 protected FPDF $fpdf;
36
37 /**
38 * QRFpdf constructor.
39 *
40 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
41 */
42 public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){
43
44 if(!class_exists(FPDF::class)){
45 // @codeCoverageIgnoreStart
46 throw new QRCodeOutputException(
47 'The QRFpdf output requires FPDF (https://github.com/Setasign/FPDF)'.
48 // phpcs:ignore
49 ' as dependency but the class "\\FPDF" could not be found.'
50 );
51 // @codeCoverageIgnoreEnd
52 }
53
54 parent::__construct($options, $matrix);
55 }
56
57 /**
58 * Initializes an FPDF instance
59 */
60 protected function initFPDF():FPDF{
61 $fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, $this->getOutputDimensions());
62 $fpdf->AddPage();
63
64 return $fpdf;
65 }
66
67 public function dump(string|null $file = null, FPDF|null $fpdf = null):string|FPDF{
68 $this->fpdf = ($fpdf ?? $this->initFPDF());
69
70 if($this::moduleValueIsValid($this->options->bgColor)){
71 $bgColor = $this->prepareModuleValue($this->options->bgColor);
72 [$width, $height] = $this->getOutputDimensions();
73 /** @phan-suppress-next-line PhanParamTooFewUnpack */
74 $this->fpdf->SetFillColor(...$bgColor);
75 $this->fpdf->Rect(0, 0, $width, $height, 'F');
76 }
77
78 $this->prevColor = null;
79
80 foreach($this->matrix->getMatrix() as $y => $row){
81 foreach($row as $x => $M_TYPE){
82 $this->module($x, $y, $M_TYPE);
83 }
84 }
85
86 if($this->options->returnResource){
87 return $this->fpdf;
88 }
89
90 $pdfData = $this->fpdf->Output('S');
91
92 $this->saveToFile($pdfData, $file);
93
94 if($this->options->outputBase64){
95 $pdfData = $this->toBase64DataURI($pdfData);
96 }
97
98 return $pdfData;
99 }
100
101 /**
102 * Renders a single module
103 */
104 protected function module(int $x, int $y, int $M_TYPE):void{
105
106 if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
107 return;
108 }
109
110 $color = $this->getModuleValue($M_TYPE);
111
112 if($color !== null && $color !== $this->prevColor){
113 /** @phan-suppress-next-line PhanParamTooFewUnpack */
114 $this->fpdf->SetFillColor(...$color);
115 $this->prevColor = $color;
116 }
117
118 $this->fpdf->Rect(($x * $this->scale), ($y * $this->scale), $this->scale, $this->scale, 'F');
119 }
120
121 }