PHP code example of octacrafts / oc-qr

1. Go to this page and download the library: Download octacrafts/oc-qr 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/ */

    

octacrafts / oc-qr example snippets




ctacrafts\QrEngine\Core\QrBuilder;
use Octacrafts\QrEngine\Domain\ErrorCorrectionLevel;
use Octacrafts\QrEngine\Domain\OutputFormat;

$output = QrBuilder::create('https://example.com')
    ->errorCorrection(ErrorCorrectionLevel::M)
    ->size(300)
    ->margin(4)
    ->format(OutputFormat::Png)
    ->generate();

file_put_contents('qr.png', $output->content());

$svg = QrBuilder::create('Hello, World!')
    ->format(OutputFormat::Svg)
    ->size(300)
    ->margin(4)
    ->generate();

file_put_contents('qr.svg', $svg->content());

return response($output->content())
    ->header('Content-Type', $output->contentType());

$output->content();      // Raw bytes (PNG) or XML string (SVG)
$output->format();       // OutputFormat enum
$output->contentType(); // e.g. image/png, image/svg+xml

use Octacrafts\QrEngine\Core\QrEngineFactory;
use Octacrafts\QrEngine\Domain\ErrorCorrectionLevel;
use Octacrafts\QrEngine\Domain\GenerationOptions;
use Octacrafts\QrEngine\Domain\OutputFormat;
use Octacrafts\QrEngine\Domain\Payload;
use Octacrafts\QrEngine\Domain\QrVersion;
use Octacrafts\QrEngine\Domain\RenderOptions;

$engine = QrEngineFactory::createDefault();

$output = $engine->generate(
    Payload::fromString('https://example.com'),
    (new GenerationOptions(ErrorCorrectionLevel::M))
        ->withVersion(QrVersion::fromNumber(2))  // optional: force version 1–40
        ->withMask(2),                            // optional: force mask 0–7
    new RenderOptions(
        size: 400,
        margin: 4,
        foreground: '#000000',
        background: '#ffffff',
    ),
    OutputFormat::Png,
);
bash
php examples/generate.php