chillerlan / php-qrcode

A PHP QR Code generator and reader with a user-friendly API.
https://smiley.codes/qrcode/
Apache License 2.0
2k stars 297 forks source link

Custom pixel shape #71

Closed saulgonzalez76 closed 3 years ago

saulgonzalez76 commented 3 years ago

Hi, I want to create custom pixel shapes, some like this closed issue https://github.com/chillerlan/php-qrcode/issues/23

But I'm lost cant make it work. Could you please help me on this ? My code is :

<?php
/**
 *
 * @filesource   image.php
 * @created      24.12.2017
 * @author       Smiley <smiley@chillerlan.net>
 * @copyright    2017 Smiley
 * @license      MIT
 */

namespace chillerlan\QRCodeExamples;
use chillerlan\QRCode\{Output\QRImage, QRCode, QROptions};
require_once '../../vendor/autoload.php';
$codigo = filter_input(INPUT_GET, 'codigo');
$gzip = true;

class MyCustomOutput extends QRImage{
    // inherited from QROutputAbstract
    protected function setPixel(int $x, int $y, array $rgb):void{
        imagefilledellipse(
            $this->image,
            ($x * $this->scale) + ($this->scale / 2),
            ($y * $this->scale) + ($this->scale / 2),
            $this->scale-1,
            $this->scale-1,
            imagecolorallocate($this->image, ...$rgb)
        );
    }
}

$options = new QROptions([
    'version'      => 5,
    'outputType'   => QRCode::OUTPUT_MARKUP_SVG,
    'imageBase64'  => false,
    'eccLevel'     => QRCode::ECC_L,
    'outputInterface' => MyCustomOutput::class,
    'addQuietzone' => true,
    'svgOpacity'   => 1.0,
    'svgDefs'      => '
        <linearGradient id="g1" x1="106%" y1="94%" x2="0%" y2="0%">
            <stop offset="0%" style="stop-color:rgb(0,88,255);stop-opacity:1;" />
            <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1;" />
        </linearGradient>
        <style>rect{shape-rendering:crispEdges}</style>',
    'moduleValues' => [
        // finder
        1536 => 'url(#g1)', // dark (true)  #093480   #BD2E02
        6    => '#fff', // light (false)
        // alignment
        2560 => 'url(#g1)',
        10   => '#fff',
        // timing
        3072 => 'url(#g1)',
        12   => '#fff',
        // format
        3584 => 'url(#g1)',
        14   => '#fff',
        // version
        4096 => 'url(#g1)',
        16   => '#fff',
        // data
        1024 => 'url(#g1)',
        4    => '#fff',
        // darkmodule
        512  => 'url(#g1)',
        // separator
        8    => '#fff',
        // quietzone
        18   => '#fff',
    ],
]);

$qrcode = (new QRCode($options))->render($codigo);

header('Content-type: image/svg+xml');

if($gzip === true){
    header('Vary: Accept-Encoding');
    header('Content-Encoding: gzip');
    $qrcode = gzencode($qrcode ,9);
}
echo $qrcode;

Thanks !

codemasher commented 3 years ago

Hi, you need to set QROptions::$outputType to QRCode::OUTPUT_IMAGE_PNG (or jpg/gif) for that to work. The SVG output works entirely different and i'm afraid i'm not able to help with that.