|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * (c) Jeroen van den Enden <[email protected]> |
| 7 | + * |
| 8 | + * This source file is subject to the MIT license that is bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Endroid\QrCode\Writer; |
| 13 | + |
| 14 | +use Endroid\QrCode\Exception\GenerateImageException; |
| 15 | +use Endroid\QrCode\Exception\ValidationException; |
| 16 | +use Endroid\QrCode\QrCodeInterface; |
| 17 | + |
| 18 | +class FpdfWriter extends AbstractWriter |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Defines as which unit the size is handled. Default is: "mm" |
| 22 | + * |
| 23 | + * Allowed values: 'mm', 'pt', 'cm', 'in' |
| 24 | + */ |
| 25 | + public const WRITER_OPTION_MEASURE_UNIT = 'fpdf_measure_unit'; |
| 26 | + |
| 27 | + public function __construct() |
| 28 | + { |
| 29 | + if(!\class_exists(\FPDF::class)){ |
| 30 | + throw new \BadMethodCallException( |
| 31 | + 'The Fpdf writer requires FPDF as dependency but the class "\\FPDF" couldn\'t be found.' |
| 32 | + ); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public function writeString(QrCodeInterface $qrCode): string |
| 37 | + { |
| 38 | + if ($qrCode->getValidateResult()) { |
| 39 | + throw new ValidationException( |
| 40 | + 'Built-in validation reader can not check fpdf qr codes: please disable via setValidateResult(false)' |
| 41 | + ); |
| 42 | + } |
| 43 | + $foregroundColor = $qrCode->getForegroundColor(); |
| 44 | + if ($foregroundColor['a'] !== 0) { |
| 45 | + throw new \InvalidArgumentException( |
| 46 | + 'The foreground color has an alpha channel, but the fpdf qr writer doesn\'t support alpha channels.' |
| 47 | + ); |
| 48 | + } |
| 49 | + $backgroundColor = $qrCode->getBackgroundColor(); |
| 50 | + if ($backgroundColor['a'] !== 0) { |
| 51 | + throw new \InvalidArgumentException( |
| 52 | + 'The foreground color has an alpha channel, but the fpdf qr writer doesn\'t support alpha channels.' |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + $label = $qrCode->getLabel(); |
| 57 | + if (null !== $label) { |
| 58 | + throw new \InvalidArgumentException('The fpdf qr writer doesn\'t support a label.'); |
| 59 | + } |
| 60 | + |
| 61 | + $data = $qrCode->getData(); |
| 62 | + $options = $qrCode->getWriterOptions(); |
| 63 | + |
| 64 | + $fpdf = new \FPDF( |
| 65 | + 'P', |
| 66 | + $options[self::WRITER_OPTION_MEASURE_UNIT] ?? 'mm', |
| 67 | + [$data['outer_width'], $data['outer_height']] |
| 68 | + ); |
| 69 | + $fpdf->AddPage(); |
| 70 | + |
| 71 | + $fpdf->SetFillColor($backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']); |
| 72 | + $fpdf->Rect(0, 0, $data['outer_width'], $data['outer_height'], 'F'); |
| 73 | + |
| 74 | + $fpdf->SetFillColor($foregroundColor['r'], $foregroundColor['g'], $foregroundColor['b']); |
| 75 | + foreach ($data['matrix'] as $row => $values) { |
| 76 | + foreach ($values as $column => $value) { |
| 77 | + if (1 === $value) { |
| 78 | + $fpdf->Rect( |
| 79 | + $data['margin_left'] + ($column * $data['block_size']), |
| 80 | + $data['margin_left'] + ($row * $data['block_size']), |
| 81 | + $data['block_size'], |
| 82 | + $data['block_size'], |
| 83 | + 'F' |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + $logoPath = $qrCode->getLogoPath(); |
| 90 | + if (null !== $logoPath) { |
| 91 | + $this->addLogo( |
| 92 | + $fpdf, |
| 93 | + $logoPath, |
| 94 | + $qrCode->getLogoWidth(), |
| 95 | + $qrCode->getLogoHeight(), |
| 96 | + $data['outer_width'], |
| 97 | + $data['outer_height'] |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return $fpdf->Output('S'); |
| 102 | + } |
| 103 | + |
| 104 | + protected function addLogo( |
| 105 | + \FPDF $fpdf, |
| 106 | + string $logoPath, |
| 107 | + ?int $logoWidth, |
| 108 | + ?int $logoHeight, |
| 109 | + int $imageWidth, |
| 110 | + int $imageHeight |
| 111 | + ) { |
| 112 | + if (null === $logoHeight || null === $logoWidth) { |
| 113 | + [$logoSourceWidth, $logoSourceHeight] = \getimagesize($logoPath); |
| 114 | + |
| 115 | + if (null === $logoWidth) { |
| 116 | + $logoWidth = (int) $logoSourceWidth; |
| 117 | + } |
| 118 | + |
| 119 | + if (null === $logoHeight) { |
| 120 | + $aspectRatio = $logoWidth / $logoSourceWidth; |
| 121 | + $logoHeight = (int) ($logoSourceHeight * $aspectRatio); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + $logoX = $imageWidth / 2 - (int) $logoWidth / 2; |
| 126 | + $logoY = $imageHeight / 2 - (int) $logoHeight / 2; |
| 127 | + |
| 128 | + $fpdf->Image($logoPath, $logoX, $logoY, $logoWidth, $logoHeight); |
| 129 | + } |
| 130 | + |
| 131 | + public static function getContentType(): string |
| 132 | + { |
| 133 | + return 'application/pdf'; |
| 134 | + } |
| 135 | + |
| 136 | + public static function getSupportedExtensions(): array |
| 137 | + { |
| 138 | + return ['pdf']; |
| 139 | + } |
| 140 | + |
| 141 | + public function getName(): string |
| 142 | + { |
| 143 | + return 'fpdf'; |
| 144 | + } |
| 145 | +} |
0 commit comments