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