Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/Writer/GdTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ public function writeGd(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?L
for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) {
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) {
if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) {
imagefilledrectangle(
$this->addBlock(
$baseImage,
$columnIndex * $baseBlockSize,
$rowIndex * $baseBlockSize,
($columnIndex + 1) * $baseBlockSize - 1,
($rowIndex + 1) * $baseBlockSize - 1,
$rowIndex,
$columnIndex,
$baseBlockSize,
$foregroundColor
);
}
Expand Down Expand Up @@ -230,4 +229,25 @@ public function validateResult(ResultInterface $result, string $expectedData): v
throw ValidationException::createForInvalidData($expectedData, strval($reader->text()));
}
}

/**
* Draws a single square (block) to the image.
* Can be overwritten to use custom shapes instead of squares.
Copy link
Copy Markdown
Owner

@endroid endroid May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Copilot what about composition over inheritance, why would I need to allow to extend it

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you asking me or copilot?

In case: I was just looking for a simple way to replace the block rendering with custom code and did not come up with a better solution (without code duplication)

*/
public function addBlock(
\GdImage $baseImage,
int $rowIndex,
int $columnIndex,
int $baseBlockSize,
int $foregroundColor
): void {
Comment on lines +237 to +243
imagefilledrectangle(
$baseImage,
$columnIndex * $baseBlockSize,
$rowIndex * $baseBlockSize,
($columnIndex + 1) * $baseBlockSize - 1,
($rowIndex + 1) * $baseBlockSize - 1,
$foregroundColor
);
}
}
2 changes: 1 addition & 1 deletion src/Writer/PngWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Endroid\QrCode\Writer\Result\PngResult;
use Endroid\QrCode\Writer\Result\ResultInterface;

final readonly class PngWriter implements WriterInterface, ValidatingWriterInterface
readonly class PngWriter implements WriterInterface, ValidatingWriterInterface
{
use GdTrait;

Expand Down