|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class QRFpdf |
| 4 | + * |
| 5 | + * https://github.com/chillerlan/php-qrcode/pull/49 |
| 6 | + * |
| 7 | + * @filesource QRFpdf.php |
| 8 | + * @created 03.06.2020 |
| 9 | + * @package chillerlan\QRCode\Output |
| 10 | + * @author Maximilian Kresse |
| 11 | + * |
| 12 | + * @license MIT |
| 13 | + */ |
| 14 | + |
| 15 | +namespace chillerlan\QRCode\Output; |
| 16 | + |
| 17 | +use chillerlan\QRCode\Data\QRMatrix; |
| 18 | +use chillerlan\QRCode\QRCodeException; |
| 19 | +use chillerlan\Settings\SettingsContainerInterface; |
| 20 | +use FPDF; |
| 21 | + |
| 22 | +use function array_values, class_exists, count, is_array; |
| 23 | + |
| 24 | +/** |
| 25 | + * QRFpdf output module (requires fpdf) |
| 26 | + * |
| 27 | + * @see https://github.com/Setasign/FPDF |
| 28 | + * @see http://www.fpdf.org/ |
| 29 | + */ |
| 30 | +class QRFpdf extends QROutputAbstract{ |
| 31 | + |
| 32 | + public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){ |
| 33 | + |
| 34 | + if(!class_exists(FPDF::class)){ |
| 35 | + // @codeCoverageIgnoreStart |
| 36 | + throw new QRCodeException( |
| 37 | + 'The QRFpdf output requires FPDF as dependency but the class "\FPDF" couldn\'t be found.' |
| 38 | + ); |
| 39 | + // @codeCoverageIgnoreEnd |
| 40 | + } |
| 41 | + |
| 42 | + parent::__construct($options, $matrix); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritDoc |
| 47 | + */ |
| 48 | + protected function setModuleValues():void{ |
| 49 | + |
| 50 | + foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){ |
| 51 | + $v = $this->options->moduleValues[$M_TYPE] ?? null; |
| 52 | + |
| 53 | + if(!is_array($v) || count($v) < 3){ |
| 54 | + $this->moduleValues[$M_TYPE] = $defaultValue |
| 55 | + ? [0, 0, 0] |
| 56 | + : [255, 255, 255]; |
| 57 | + } |
| 58 | + else{ |
| 59 | + $this->moduleValues[$M_TYPE] = array_values($v); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @inheritDoc |
| 68 | + */ |
| 69 | + public function dump(string $file = null):string{ |
| 70 | + $file = $file ?? $this->options->cachefile; |
| 71 | + |
| 72 | + $fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]); |
| 73 | + $fpdf->AddPage(); |
| 74 | + |
| 75 | + $prevColor = null; |
| 76 | + |
| 77 | + foreach($this->matrix->matrix() as $y => $row){ |
| 78 | + |
| 79 | + foreach($row as $x => $M_TYPE){ |
| 80 | + /** @var int $M_TYPE */ |
| 81 | + $color = $this->moduleValues[$M_TYPE]; |
| 82 | + |
| 83 | + if($prevColor === null || $prevColor !== $color){ |
| 84 | + $fpdf->SetFillColor(...$color); |
| 85 | + $prevColor = $color; |
| 86 | + } |
| 87 | + |
| 88 | + $fpdf->Rect($x * $this->scale, $y * $this->scale, 1 * $this->scale, 1 * $this->scale, 'F'); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + $pdfData = $fpdf->Output('S'); |
| 94 | + |
| 95 | + if($file !== null){ |
| 96 | + $this->saveToFile($pdfData, $file); |
| 97 | + } |
| 98 | + |
| 99 | + return $pdfData; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments