Skip to content

Commit bcd410a

Browse files
committed
:octocat: cleanup, align module() method between the several output classes where applicable
1 parent f8d553b commit bcd410a

File tree

5 files changed

+105
-78
lines changed

5 files changed

+105
-78
lines changed

src/Output/QREps.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function getDefaultModuleValue(bool $isDark):string{
7676
}
7777

7878
/**
79-
* Set the color format
79+
* Set the color format string
8080
*
8181
* 4 values in the color array will be interpreted as CMYK, 3 as RGB
8282
*
@@ -121,13 +121,13 @@ public function dump(string $file = null):string{
121121
'%%EndProlog',
122122
];
123123

124-
if($this->options->bgColor !== null && self::moduleValueIsValid($this->options->bgColor)){
124+
if($this::moduleValueIsValid($this->options->bgColor)){
125125
$eps[] = $this->prepareModuleValue($this->options->bgColor);
126126
$eps[] = sprintf('0 0 %1$s %1$s F', $this->length);
127127
}
128128

129129
// create the path elements
130-
$paths = $this->collectModules(fn(int $x, int $y):string => $this->module($x, $y));
130+
$paths = $this->collectModules(fn(int $x, int $y, int $M_TYPE):string => $this->module($x, $y, $M_TYPE));
131131

132132
foreach($paths as $M_TYPE => $path){
133133

@@ -150,9 +150,9 @@ public function dump(string $file = null):string{
150150
}
151151

152152
/**
153-
* returns a path segment for a single module
153+
* Returns a path segment for a single module
154154
*/
155-
protected function module(int $x, int $y):string{
155+
protected function module(int $x, int $y, int $M_TYPE):string{
156156

157157
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
158158
return '';

src/Output/QRFpdf.php

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
*/
2626
class QRFpdf extends QROutputAbstract{
2727

28+
protected FPDF $fpdf;
29+
protected ?array $prevColor = null;
30+
2831
/**
2932
* QRFpdf constructor.
3033
*
@@ -73,6 +76,7 @@ public static function moduleValueIsValid($value):bool{
7376
* @param array $value
7477
*
7578
* @inheritDoc
79+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
7680
*/
7781
protected function prepareModuleValue($value):array{
7882
$values = [];
@@ -86,6 +90,10 @@ protected function prepareModuleValue($value):array{
8690
$values[] = max(0, min(255, intval($val)));
8791
}
8892

93+
if(count($values) !== 3){
94+
throw new QRCodeOutputException('invalid color value');
95+
}
96+
8997
return $values;
9098
}
9199

@@ -96,50 +104,42 @@ protected function getDefaultModuleValue(bool $isDark):array{
96104
return ($isDark) ? [0, 0, 0] : [255, 255, 255];
97105
}
98106

107+
/**
108+
* Initializes an FPDF instance
109+
*/
110+
protected function initFPDF():FPDF{
111+
return new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
112+
}
113+
99114
/**
100115
* @inheritDoc
101116
*
102117
* @return string|\FPDF
103118
*/
104119
public function dump(string $file = null){
105-
106-
$fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
107-
$fpdf->AddPage();
120+
$this->fpdf = $this->initFPDF();
121+
$this->fpdf->AddPage();
108122

109123
if($this::moduleValueIsValid($this->options->bgColor)){
110124
$bgColor = $this->prepareModuleValue($this->options->bgColor);
111125
/** @phan-suppress-next-line PhanParamTooFewUnpack */
112-
$fpdf->SetFillColor(...$bgColor);
113-
$fpdf->Rect(0, 0, $this->length, $this->length, 'F');
126+
$this->fpdf->SetFillColor(...$bgColor);
127+
$this->fpdf->Rect(0, 0, $this->length, $this->length, 'F');
114128
}
115129

116-
$prevColor = null;
117-
118-
for($y = 0; $y < $this->moduleCount; $y++){
119-
for($x = 0; $x < $this->moduleCount; $x++){
120-
121-
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
122-
continue;
123-
}
124-
125-
$color = $this->getModuleValueAt($x, $y);
126-
127-
if($color !== null && $color !== $prevColor){
128-
/** @phan-suppress-next-line PhanParamTooFewUnpack */
129-
$fpdf->SetFillColor(...$color);
130-
$prevColor = $color;
131-
}
130+
$this->prevColor = null;
132131

133-
$fpdf->Rect(($x * $this->scale), ($y * $this->scale), $this->scale, $this->scale, 'F');
132+
foreach($this->matrix->getMatrix() as $y => $row){
133+
foreach($row as $x => $M_TYPE){
134+
$this->module($x, $y, $M_TYPE);
134135
}
135-
136136
}
137137

138138
if($this->options->returnResource){
139-
return $fpdf;
139+
return $this->fpdf;
140140
}
141141

142-
$pdfData = $fpdf->Output('S');
142+
$pdfData = $this->fpdf->Output('S');
143143

144144
$this->saveToFile($pdfData, $file);
145145

@@ -150,4 +150,24 @@ public function dump(string $file = null){
150150
return $pdfData;
151151
}
152152

153+
/**
154+
* Renders a single module
155+
*/
156+
protected function module(int $x, int $y, int $M_TYPE):void{
157+
158+
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
159+
return;
160+
}
161+
162+
$color = $this->getModuleValue($M_TYPE);
163+
164+
if($color !== null && $color !== $this->prevColor){
165+
/** @phan-suppress-next-line PhanParamTooFewUnpack */
166+
$this->fpdf->SetFillColor(...$color);
167+
$this->prevColor = $color;
168+
}
169+
170+
$this->fpdf->Rect(($x * $this->scale), ($y * $this->scale), $this->scale, $this->scale, 'F');
171+
}
172+
153173
}

src/Output/QRGdImage.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* Converts the matrix into GD images, raw or base64 output (requires ext-gd)
2424
*
25-
* @see http://php.net/manual/book.image.php
25+
* @see https://php.net/manual/book.image.php
2626
*/
2727
class QRGdImage extends QROutputAbstract{
2828

@@ -228,41 +228,45 @@ protected function setTransparencyColor():void{
228228
* Creates the QR image
229229
*/
230230
protected function drawImage():void{
231-
for($y = 0; $y < $this->moduleCount; $y++){
232-
for($x = 0; $x < $this->moduleCount; $x++){
233-
$this->setPixel($x, $y);
231+
foreach($this->matrix->getMatrix() as $y => $row){
232+
foreach($row as $x => $M_TYPE){
233+
$this->module($x, $y, $M_TYPE);
234234
}
235235
}
236236
}
237237

238238
/**
239239
* Creates a single QR pixel with the given settings
240240
*/
241-
protected function setPixel(int $x, int $y):void{
241+
protected function module(int $x, int $y, int $M_TYPE):void{
242242

243243
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
244244
return;
245245
}
246246

247-
$color = $this->getModuleValueAt($x, $y);
247+
$color = $this->getModuleValue($M_TYPE);
248248

249-
$this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)
250-
? imagefilledellipse(
249+
if($this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)){
250+
imagefilledellipse(
251251
$this->image,
252252
(($x * $this->scale) + intdiv($this->scale, 2)),
253253
(($y * $this->scale) + intdiv($this->scale, 2)),
254254
(int)(2 * $this->options->circleRadius * $this->scale),
255255
(int)(2 * $this->options->circleRadius * $this->scale),
256256
$color
257-
)
258-
: imagefilledrectangle(
259-
$this->image,
260-
($x * $this->scale),
261-
($y * $this->scale),
262-
(($x + 1) * $this->scale),
263-
(($y + 1) * $this->scale),
264-
$color
265257
);
258+
259+
return;
260+
}
261+
262+
imagefilledrectangle(
263+
$this->image,
264+
($x * $this->scale),
265+
($y * $this->scale),
266+
(($x + 1) * $this->scale),
267+
(($y + 1) * $this->scale),
268+
$color
269+
);
266270
}
267271

268272
/**

src/Output/QRImagick.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
/**
2222
* ImageMagick output module (requires ext-imagick)
2323
*
24-
* @see http://php.net/manual/book.imagick.php
25-
* @see http://phpimagick.com
24+
* @see https://php.net/manual/book.imagick.php
25+
* @see https://phpimagick.com
2626
*/
2727
class QRImagick extends QROutputAbstract{
2828

@@ -97,7 +97,6 @@ public static function moduleValueIsValid($value):bool{
9797

9898
/**
9999
* @inheritDoc
100-
* @throws \ImagickPixelException
101100
*/
102101
protected function prepareModuleValue($value):ImagickPixel{
103102
return new ImagickPixel($value);
@@ -186,9 +185,9 @@ protected function drawImage():void{
186185
$this->imagickDraw = new ImagickDraw;
187186
$this->imagickDraw->setStrokeWidth(0);
188187

189-
for($y = 0; $y < $this->moduleCount; $y++){
190-
for($x = 0; $x < $this->moduleCount; $x++){
191-
$this->setPixel($x, $y);
188+
foreach($this->matrix->getMatrix() as $y => $row){
189+
foreach($row as $x => $M_TYPE){
190+
$this->module($x, $y, $M_TYPE);
192191
}
193192
}
194193

@@ -198,27 +197,31 @@ protected function drawImage():void{
198197
/**
199198
* draws a single pixel at the given position
200199
*/
201-
protected function setPixel(int $x, int $y):void{
200+
protected function module(int $x, int $y, int $M_TYPE):void{
202201

203202
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
204203
return;
205204
}
206205

207-
$this->imagickDraw->setFillColor($this->getModuleValueAt($x, $y));
206+
$this->imagickDraw->setFillColor($this->getModuleValue($M_TYPE));
208207

209-
$this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)
210-
? $this->imagickDraw->circle(
208+
if($this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)){
209+
$this->imagickDraw->circle(
211210
(($x + 0.5) * $this->scale),
212211
(($y + 0.5) * $this->scale),
213212
(($x + 0.5 + $this->options->circleRadius) * $this->scale),
214213
(($y + 0.5) * $this->scale)
215-
)
216-
: $this->imagickDraw->rectangle(
217-
($x * $this->scale),
218-
($y * $this->scale),
219-
(($x + 1) * $this->scale),
220-
(($y + 1) * $this->scale)
221214
);
215+
216+
return;
217+
}
218+
219+
$this->imagickDraw->rectangle(
220+
($x * $this->scale),
221+
($y * $this->scale),
222+
(($x + 1) * $this->scale),
223+
(($y + 1) * $this->scale)
224+
);
222225
}
223226

224227
}

src/Output/QRMarkupHTML.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace chillerlan\QRCode\Output;
1212

13-
use function sprintf;
13+
use function implode, sprintf;
1414

1515
/**
1616
* HTML output (a cheap markup substitute when SVG is not available or not an option)
@@ -21,29 +21,29 @@ class QRMarkupHTML extends QRMarkup{
2121
* @inheritDoc
2222
*/
2323
protected function createMarkup(bool $saveToFile):string{
24-
$html = empty($this->options->cssClass)
25-
? '<div>'
26-
: sprintf('<div class="%s">', $this->getCssClass());
24+
$rows = [];
2725

28-
$html .= $this->options->eol;
26+
foreach($this->matrix->getMatrix() as $row){
27+
$element = '<span style="background: %s;"></span>';
28+
$modules = array_map(fn(int $M_TYPE):string => sprintf($element, $this->getModuleValue($M_TYPE)), $row);
2929

30-
for($y = 0; $y < $this->moduleCount; $y++){
31-
$html .= '<div>';
32-
33-
for($x = 0; $x < $this->moduleCount; $x++){
34-
$html .= sprintf('<span style="background: %s;"></span>', $this->getModuleValueAt($x, $y));
35-
}
36-
37-
$html .= '</div>'.$this->options->eol;
30+
$rows[] = sprintf('<div>%s</div>%s', implode('', $modules), $this->options->eol);
3831
}
3932

40-
$html .= '</div>'.$this->options->eol;
33+
$html = sprintf(
34+
'<div class="%1$s">%3$s%2$s</div>%3$s',
35+
$this->getCssClass(),
36+
implode('', $rows),
37+
$this->options->eol
38+
);
4139

4240
// wrap the snippet into a body when saving to file
4341
if($saveToFile){
4442
$html = sprintf(
45-
'<!DOCTYPE html><html lang=""><head><meta charset="UTF-8"><title>QR Code</title></head><body>%s</body></html>',
46-
$this->options->eol.$html
43+
'<!DOCTYPE html><html lang="none">%2$s<head>%2$s<meta charset="UTF-8">%2$s'.
44+
'<title>QR Code</title></head>%2$s<body>%1$s</body>%2$s</html>',
45+
$html,
46+
$this->options->eol
4747
);
4848
}
4949

0 commit comments

Comments
 (0)