SimpleSoftwareIO / simple-qrcode

An easy-to-use PHP QrCode generator with first-party support for Laravel.
https://www.simplesoftware.io/simple-qrcode
MIT License
2.66k stars 363 forks source link

added support for custom renderers #258

Open adrum opened 1 year ago

adrum commented 1 year ago

This adds the ability to pass your own custom renderers to the Generator class for both the module and the eye.

SimplyCorey commented 1 year ago

Thanks @adrum Can you provide a few code samples with a custom module/eye just so I can test please.

adrum commented 1 year ago

Hey @SimplyCorey. I do have a custom eye, but not a custom module. Here's some ways to help test this:

       $generator
            ->size(1500)
            ->format('png')
            ->eye(\App\Utilities\SimpleQrCodeRoundedEye::class)
            ->style('round', 0.5)
            ->color(25, 29, 50)
            ->generate($url, $path);

SimpleQrCodeRoundedEye.php

<?php

namespace App\Utilities;

use BaconQrCode\Renderer\Path\Path;
use SimpleSoftwareIO\QrCode\Singleton;
use BaconQrCode\Renderer\Eye\EyeInterface;

/**
 * Renders the inner eye as a rounded square.
 */
final class SimpleQrCodeRoundedEye implements EyeInterface, Singleton
{
    /**
     * @var self|null
     */
    private static $instance;

    private function __construct()
    {
    }

    public static function instance(): self
    {
        return self::$instance ?: self::$instance = new self();
    }

    public function getExternalPath(): Path
    {
        return (new Path())
        ->move(3.5, -2.0)
        ->line(3.5, 2.0)
        ->curve(3.5, 2.75, 2.75, 3.5, 2.0, 3.5)
        ->line(-2.0, 3.5)
        ->curve(-2.75, 3.5, -3.5, 2.75, -3.5, 2.0)
        ->line(-3.5, -2.0)
        ->curve(-3.5, -2.75, -2.75, -3.5, -2.0, -3.5)
        ->line(2.0, -3.5)
        ->curve(2.75, -3.5, 3.5, -2.75, 3.5, -2.0)
        ->close()
        ->move(2.5, -1.5)
        ->line(2.5, 1.5)
        ->curve(2.5, 2.0, 2.0, 2.5, 1.5, 2.5)
        ->line(-1.5, 2.5)
        ->curve(-2.0, 2.5, -2.5, 2.0, -2.5, 1.5)
        ->line(-2.5, -1.5)
        ->curve(-2.5, -2.0, -2.0, -2.5, -1.5, -2.5)
        ->line(1.5, -2.5)
        ->curve(2.0, -2.5, 2.5, -2.0, 2.5, -1.5)
        ->close();
    }

    public function getInternalPath(): Path
    {
        return (new Path())
        ->move(1.5, -0.5)
        ->line(1.5, 0.5)
        ->curve(1.5, 1.0, 1.0, 1.5, 0.5, 1.5)
        ->line(-0.5, 1.5)
        ->curve(-1.0, 1.5, -1.5, 1.0, -1.5, 0.5)
        ->line(-1.5, -0.5)
        ->curve(-1.5, -1.0, -1.0, -1.5, -0.5, -1.5)
        ->line(0.5, -1.5)
        ->curve(1.0, -1.5, 1.5, -1.0, 1.5, -0.5)
        ->close();
    }
}

In theory, you could test the module via:

$generator->style(\BaconQrCode\Renderer\Module\RoundnessModule::class, 0.5);