PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.4.2
WP 2FA – Two-factor authentication for WordPress v2.4.2
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / vendor / endroid / qr-code / src / Writer / FpdfWriter.php
wp-2fa / vendor / endroid / qr-code / src / Writer Last commit date
AbstractWriter.php 3 years ago BinaryWriter.php 3 years ago DebugWriter.php 3 years ago EpsWriter.php 3 years ago FpdfWriter.php 3 years ago PngWriter.php 3 years ago SvgWriter.php 3 years ago WriterInterface.php 3 years ago
FpdfWriter.php
94 lines
1 <?php
2
3 declare (strict_types=1);
4 /*
5 * (c) Jeroen van den Enden <info@endroid.nl>
6 *
7 * This source file is subject to the MIT license that is bundled
8 * with this source code in the file LICENSE.
9 */
10 namespace WP2FA_Vendor\Endroid\QrCode\Writer;
11
12 use WP2FA_Vendor\Endroid\QrCode\Exception\ValidationException;
13 use WP2FA_Vendor\Endroid\QrCode\QrCodeInterface;
14 class FpdfWriter extends AbstractWriter
15 {
16 /**
17 * Defines as which unit the size is handled. Default is: "mm".
18 *
19 * Allowed values: 'mm', 'pt', 'cm', 'in'
20 */
21 public const WRITER_OPTION_MEASURE_UNIT = 'fpdf_measure_unit';
22 public function writeString(QrCodeInterface $qrCode) : string
23 {
24 if (!\class_exists(\WP2FA_Vendor\FPDF::class)) {
25 throw new \BadMethodCallException('The Fpdf writer requires FPDF as dependency but the class "\\FPDF" couldn\'t be found.');
26 }
27 if ($qrCode->getValidateResult()) {
28 throw new ValidationException('Built-in validation reader can not check fpdf qr codes: please disable via setValidateResult(false)');
29 }
30 $foregroundColor = $qrCode->getForegroundColor();
31 if (0 !== $foregroundColor['a']) {
32 throw new \InvalidArgumentException('The foreground color has an alpha channel, but the fpdf qr writer doesn\'t support alpha channels.');
33 }
34 $backgroundColor = $qrCode->getBackgroundColor();
35 if (0 !== $backgroundColor['a']) {
36 throw new \InvalidArgumentException('The foreground color has an alpha channel, but the fpdf qr writer doesn\'t support alpha channels.');
37 }
38 $label = $qrCode->getLabel();
39 $labelHeight = null !== $label ? 30 : 0;
40 $data = $qrCode->getData();
41 $options = $qrCode->getWriterOptions();
42 $fpdf = new \WP2FA_Vendor\FPDF('P', $options[self::WRITER_OPTION_MEASURE_UNIT] ?? 'mm', [$data['outer_width'], $data['outer_height'] + $labelHeight]);
43 $fpdf->AddPage();
44 $fpdf->SetFillColor($backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']);
45 $fpdf->Rect(0, 0, $data['outer_width'], $data['outer_height'], 'F');
46 $fpdf->SetFillColor($foregroundColor['r'], $foregroundColor['g'], $foregroundColor['b']);
47 foreach ($data['matrix'] as $row => $values) {
48 foreach ($values as $column => $value) {
49 if (1 === $value) {
50 $fpdf->Rect($data['margin_left'] + $column * $data['block_size'], $data['margin_left'] + $row * $data['block_size'], $data['block_size'], $data['block_size'], 'F');
51 }
52 }
53 }
54 $logoPath = $qrCode->getLogoPath();
55 if (null !== $logoPath) {
56 $this->addLogo($fpdf, $logoPath, $qrCode->getLogoWidth(), $qrCode->getLogoHeight(), $data['outer_width'], $data['outer_height']);
57 }
58 if (null !== $label) {
59 $fpdf->setY($data['outer_height'] + 5);
60 $fpdf->SetFont('Helvetica', null, $qrCode->getLabelFontSize());
61 $fpdf->Cell(0, 0, $label, 0, 0, \strtoupper($qrCode->getLabelAlignment()[0]));
62 }
63 return $fpdf->Output('S');
64 }
65 protected function addLogo(\WP2FA_Vendor\FPDF $fpdf, string $logoPath, ?int $logoWidth, ?int $logoHeight, int $imageWidth, int $imageHeight) : void
66 {
67 if (null === $logoHeight || null === $logoWidth) {
68 [$logoSourceWidth, $logoSourceHeight] = \getimagesize($logoPath);
69 if (null === $logoWidth) {
70 $logoWidth = (int) $logoSourceWidth;
71 }
72 if (null === $logoHeight) {
73 $aspectRatio = $logoWidth / $logoSourceWidth;
74 $logoHeight = (int) ($logoSourceHeight * $aspectRatio);
75 }
76 }
77 $logoX = $imageWidth / 2 - (int) $logoWidth / 2;
78 $logoY = $imageHeight / 2 - (int) $logoHeight / 2;
79 $fpdf->Image($logoPath, $logoX, $logoY, $logoWidth, $logoHeight);
80 }
81 public static function getContentType() : string
82 {
83 return 'application/pdf';
84 }
85 public static function getSupportedExtensions() : array
86 {
87 return ['pdf'];
88 }
89 public function getName() : string
90 {
91 return 'fpdf';
92 }
93 }
94