Skip to content

Commit c54d3ed

Browse files
committed
Extract addBlock in GdTrait
1 parent 5fa5348 commit c54d3ed

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

src/Writer/GdTrait.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ public function writeGd(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?L
6868
for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) {
6969
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) {
7070
if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) {
71-
imagefilledrectangle(
71+
$this->addModule(
7272
$baseImage,
73-
$columnIndex * $baseBlockSize,
74-
$rowIndex * $baseBlockSize,
75-
($columnIndex + 1) * $baseBlockSize - 1,
76-
($rowIndex + 1) * $baseBlockSize - 1,
73+
$rowIndex,
74+
$columnIndex,
75+
$baseBlockSize,
7776
$foregroundColor
7877
);
7978
}
@@ -230,4 +229,25 @@ public function validateResult(ResultInterface $result, string $expectedData): v
230229
throw ValidationException::createForInvalidData($expectedData, strval($reader->text()));
231230
}
232231
}
232+
233+
/**
234+
* Draws a single square (block) to the image.
235+
* Can be overwritten to use custom shapes instead of squares.
236+
*/
237+
public function addBlock(
238+
\GdImage $baseImage,
239+
int $rowIndex,
240+
int $columnIndex,
241+
int $baseBlockSize,
242+
int $foregroundColor
243+
): void {
244+
imagefilledrectangle(
245+
$baseImage,
246+
$columnIndex * $baseBlockSize,
247+
$rowIndex * $baseBlockSize,
248+
($columnIndex + 1) * $baseBlockSize - 1,
249+
($rowIndex + 1) * $baseBlockSize - 1,
250+
$foregroundColor
251+
);
252+
}
233253
}

src/Writer/PngWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Endroid\QrCode\Writer\Result\PngResult;
1111
use Endroid\QrCode\Writer\Result\ResultInterface;
1212

13-
final readonly class PngWriter implements WriterInterface, ValidatingWriterInterface
13+
readonly class PngWriter implements WriterInterface, ValidatingWriterInterface
1414
{
1515
use GdTrait;
1616

0 commit comments

Comments
 (0)