Skip to content

Extract addBlock in GdTrait so we can extend it to draw custom shapes#489

Open
xosofox wants to merge 1 commit intoendroid:mainfrom
xosofox:main
Open

Extract addBlock in GdTrait so we can extend it to draw custom shapes#489
xosofox wants to merge 1 commit intoendroid:mainfrom
xosofox:main

Conversation

@xosofox
Copy link
Copy Markdown

@xosofox xosofox commented May 5, 2026

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:

<?php

namespace App\Service;

use Endroid\QrCode\Writer\PngWriter;

readonly class PngShapeWriter extends PngWriter
{
    public function addBlock(
        \GdImage $baseImage,
        int $rowIndex,
        int $columnIndex,
        int $baseBlockSize,
        int $foregroundColor,
    ): void {

        // will draw a circle instead of a square
        imagefilledellipse(
            $baseImage,
            $columnIndex * $baseBlockSize + $baseBlockSize/2,
            $rowIndex * $baseBlockSize + $baseBlockSize/2,
            $baseBlockSize - 1,
            $baseBlockSize - 1,
            $foregroundColor
        );
    }
}

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 final from PngWriter to allow extending it for custom rendering.
  • Refactored GdTrait::writeGd() to delegate per-block drawing to a new addBlock() method.
  • Added a default addBlock() implementation that draws the original square block via imagefilledrectangle().

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.

Comment thread src/Writer/GdTrait.php
Comment on lines +237 to +243
public function addBlock(
\GdImage $baseImage,
int $rowIndex,
int $columnIndex,
int $baseBlockSize,
int $foregroundColor
): void {
Comment thread src/Writer/GdTrait.php

/**
* 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants