PHP code example of tonsoo / php-qr-code

1. Go to this page and download the library: Download tonsoo/php-qr-code library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tonsoo / php-qr-code example snippets




use Tonsoo\QrCode\Styling\ModuleStyle;

$svg = qrCode('https://example.com')
    ->size(640)
    ->margin(32)
    ->foreground('#5a1f14')
    ->modules(ModuleStyle::classyRounded(1.0))
    ->toSvg();



use Tonsoo\QrCode\ErrorCorrection;
use Tonsoo\QrCode\QRCodeFactory;
use Tonsoo\QrCode\Styling\EyeStyle;
use Tonsoo\QrCode\Styling\ModuleStyle;

$qrCode = (new QRCodeFactory())
    ->errorCorrection(ErrorCorrection::High)
    ->make('https://example.com')
    ->size(768)
    ->background('#ffffff')
    ->modules(
        ModuleStyle::classyRounded(1.0)
            ->color('#670908')
    )
    ->eyes(
        EyeStyle::square()
            ->outerColor('#4e0201')
            ->innerColor('#4e0201')
    );

$qrCode->save('qr.svg');
$qrCode->save('qr.png');



use Tonsoo\QrCode\Geometry\Path;
use Tonsoo\QrCode\Shapes\ShapeContext;
use Tonsoo\QrCode\Shapes\ShapeSafety;
use Tonsoo\QrCode\Styling\ModuleStyle;

$style = ModuleStyle::custom(
    callback: function (ShapeContext $context): Path {
        $x = $context->x;
        $y = $context->y;
        $s = $context->size;

        return new Path("M{$x} {$y}h{$s}v{$s}h-{$s}z");
    },
    safety: new ShapeSafety(minimumCoverageRatio: 0.7, supportsRasterRendering: false),
    name: 'my-shape',
);



use Tonsoo\QrCode\Styling\Logo;

$logo = Logo::fromPath('logo.svg')
    ->size(0.16)
    ->padding(18)
    ->background('#ffffff');
bash
sudo apt install php-gd
bash
php examples/cli.php \
  --data="https://example.com" \
  --output="qr.svg" \
  --modules-shape="classy-rounded" \
  --modules-roundness=1.0 \
  --foreground="#670908"
bash
php examples/cli.php \
  --data="https://example.com" \
  --output="qr.svg" \
  --error-correction=H \
  --modules-shape="classy-rounded" \
  --modules-roundness=1.0 \
  --logo="./logo.svg" \
  --logo-size=0.16 \
  --logo-padding=18 \
  --svg-logo-mode="inline-svg"
bash
php examples/cli.php --help