Extract addBlock in GdTrait so we can extend it to draw custom shapes#489
Extract addBlock in GdTrait so we can extend it to draw custom shapes#489xosofox wants to merge 1 commit intoendroid:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enables customization of how individual QR “blocks” are rendered in GD-based writers by extracting the block-drawing logic into an overridable method, and by making PngWriter extendable so consumers can implement custom shapes (e.g., circles).
Changes:
- Removed
finalfromPngWriterto allow extending it for custom rendering. - Refactored
GdTrait::writeGd()to delegate per-block drawing to a newaddBlock()method. - Added a default
addBlock()implementation that draws the original square block viaimagefilledrectangle().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Writer/PngWriter.php |
Makes the PNG writer extendable to support custom writers overriding block rendering. |
src/Writer/GdTrait.php |
Extracts block rendering into addBlock() so subclasses can customize the rendered shape per matrix cell. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public function addBlock( | ||
| \GdImage $baseImage, | ||
| int $rowIndex, | ||
| int $columnIndex, | ||
| int $baseBlockSize, | ||
| int $foregroundColor | ||
| ): void { |
|
|
||
| /** | ||
| * Draws a single square (block) to the image. | ||
| * Can be overwritten to use custom shapes instead of squares. |
There was a problem hiding this comment.
@Copilot what about composition over inheritance, why would I need to allow to extend it
There was a problem hiding this comment.
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)
As QR Codes do work with circles or partly rounded shapes, extracting the "addBlock" method allows to create custom writers, using the GdTrait but overwriting the addBlock method
example: