1<?php
2
3namespace Mpdf\QrCode\Output;
4
5use Mpdf\Mpdf as MpdfObject;
6use Mpdf\QrCode\QrCode;
7
8class Mpdf
9{
10
11	/**
12	 * Write the QR code into an Mpdf\Mpdf object
13	 *
14	 * @param \Mpdf\QrCode\QrCode $qrCode QR code instance
15	 * @param \Mpdf\Mpdf $mpdf Mpdf instance
16	 * @param float $x position X
17	 * @param float $y position Y
18	 * @param float $w QR code width
19	 * @param int[] $background RGB background color
20	 * @param int[] $color RGB foreground and border color
21	 */
22	public function output(QrCode $qrCode, MpdfObject $mpdf, $x, $y, $w, $background = [255, 255, 255], $color = [0, 0, 0])
23	{
24		$size = $w;
25		$qrSize = $qrCode->getQrSize();
26		$s = $size / $qrCode->getQrDimensions();
27
28		$mpdf->SetDrawColor($color[0], $color[1], $color[2]);
29		$mpdf->SetFillColor($background[0], $background[1], $background[2]);
30
31		if ($qrCode->isBorderDisabled()) {
32			$minSize = 4;
33			$maxSize = $qrSize - 4;
34			$mpdf->Rect($x, $y, $size, $size, 'F');
35		} else {
36			$minSize = 0;
37			$maxSize = $qrSize;
38			$mpdf->Rect($x, $y, $size, $size, 'FD');
39		}
40
41		$mpdf->SetFillColor($color[0], $color[1], $color[2]);
42
43		$final = $qrCode->getFinal();
44
45		for ($j = $minSize; $j < $maxSize; $j++) {
46			for ($i = $minSize; $i < $maxSize; $i++) {
47				if ($final[$i + $j * $qrSize + 1]) {
48					$mpdf->Rect($x + ($i - $minSize) * $s, $y + ($j - $minSize) * $s, $s, $s, 'F');
49				}
50			}
51		}
52	}
53
54}
55