Skip to content

Commit

Permalink
config/main: forms creation; applying options to qrcode generation
Browse files Browse the repository at this point in the history
  • Loading branch information
2amjsouza committed Aug 31, 2023
1 parent c16f9bb commit 2dc44ff
Show file tree
Hide file tree
Showing 37 changed files with 26,687 additions and 89 deletions.
12 changes: 8 additions & 4 deletions app/Livewire/QrCodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace App\Livewire;

use App\Factories\QrCodeFactory;
use App\Enum\FormatEnum;
use App\Livewire\Traits\ColorsTrait;
use App\Livewire\Traits\LabelTrait;
use App\Livewire\Traits\LogoTrait;
use App\Livewire\Traits\MarginTrait;
use App\Livewire\Traits\OptionsTrait;
use Da\QrCode\QrCode;
use Da\QrCode\Writer\JpgWriter;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;

class QrCodeBuilder extends Component
{
use ColorsTrait;
use LabelTrait;
use MarginTrait;
use LogoTrait;
use OptionsTrait;

public Collection $formats;
Expand Down
82 changes: 76 additions & 6 deletions app/Livewire/QrCodeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use App\Enum\FormatEnum;
use App\Factories\QrCodeFactory;
use Da\QrCode\Label;
use Da\QrCode\QrCode;
use Da\QrCode\Writer\JpgWriter;
use Illuminate\Support\Facades\Storage;
use Livewire\Attributes\Modelable;
use Livewire\Attributes\On;
use Livewire\Component;

Expand All @@ -16,9 +16,12 @@ class QrCodeComponent extends Component
protected QrCode $qrCode;
public int $format;
public array $options;
public ?array $colors = null;
public ?array $margins = null;
public ?string $label = null;
public array $colors = [
'background' => null,
'foreground' => null,
];
public ?int $margins = null;
public ?array $label = null;
public ?string $logoPath = null;

public function mount()
Expand All @@ -32,17 +35,84 @@ public function mount()
}

#[On('create-qr-code')]
public function create(int $format, array $options, ?array $colors = null)
public function create(int $format, array $options)
{
$this->format = $format;
$this->options = $options;

$this->build();
}

#[On('apply-colors')]
public function applyColors(array $foreground, array $background)
{
$this->colors = [
'background' => $background,
'foreground' => $foreground,
];

$this->build();
}

#[On('apply-label')]
public function applyLabel(string $label, int $size, string $alignment)
{
$this->label = [
'label' => $label,
'size' => $size,
'align' => $alignment,
];

$this->build();
}

#[On('apply-margin')]
public function applyMargin(int $margins)
{
$this->margins = $margins;

$this->build();
}

#[On('apply-logo')]
public function applyLogo(string $path)
{
$this->logoPath = $path;

$this->build();
}

public function build()
{
$this->qrCode = QrCodeFactory::build($this->format, $this->options);
/** @var QrCode $qrCode */
$qrCode = QrCodeFactory::build($this->format, $this->options);

if ($bgColors = $this->colors['background']) {
$qrCode->setBackgroundColor($bgColors['r'], $bgColors['g'], $bgColors['b']);
}

if ($fgColors = $this->colors['foreground']) {
$qrCode->setForegroundColor($fgColors['r'], $fgColors['g'], $fgColors['b']);
}

if ($label = $this->label) {
$qrCode->setLabel(new Label(
text: $label['label'],
fontSize: $label['size'],
align: $label['align'],
));
}

if ($margins = $this->margins) {
$qrCode->setMargin($margins);
}

if (! is_null($this->logoPath)) {
$qrCode->setLogo($this->logoPath)
->setLogoWidth(((int) $qrCode->getSize() * 0.16));
}

$this->qrCode = $qrCode;
}

public function getUri()
Expand Down
57 changes: 44 additions & 13 deletions app/Livewire/Traits/ColorsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,56 @@

namespace App\Livewire\Traits;

use App\Livewire\QrCodeComponent;
use Psy\Util\Str;

trait ColorsTrait
{
public string $strBackground;
public string $strForeground;

public array $background;
public array $foreground;

public function initializeColorsTrait()
{
$this->foreground = [
'r' => 0,
'g' => 0,
'b' => 0,
];
if (! isset ($this->background, $this->foreground)) {
$this->foreground = [
'r' => 0,
'g' => 0,
'b' => 0,
];

$this->background = [
'r' => 255,
'g' => 255,
'b' => 255,
];

$this->background = [
'r' => 255,
'g' => 255,
'b' => 255,
$this->strForeground = "#[{$this->foreground['r']},{$this->foreground['g']},{$this->foreground['b']}]";
$this->strBackground = "#[{$this->background['r']},{$this->background['g']},{$this->background['b']}]";
}
}

public function applyColors()
{
$this->validate($this->getColorsRules());
$this->background = $this->extractColors($this->strBackground);
$this->foreground = $this->extractColors($this->strForeground);

$this->dispatch('apply-colors', $this->foreground, $this->background)
->to(QrCodeComponent::class);
}

public function extractColors(string $color)
{
$color = preg_replace('/[\#\[\]]/', null, $color);
$color = explode(',', $color);

return [
'r' => $color[0],
'g' => $color[1],
'b' => $color[2],
];
}

Expand All @@ -28,10 +61,8 @@ public function initializeColorsTrait()
public function getColorsRules(): array
{
return [
'background' => 'required|array',
'background.*' => 'required|int',
'foreground' => 'required|array',
'foreground.*' => 'required|int',
'strForeground' => 'string|required|regex:/^\#\[\d+\,\d+\,\d+\]$/',
'strBackground' => 'string|required|regex:/^\#\[\d+\,\d+\,\d+\]$/'
];
}
}
30 changes: 30 additions & 0 deletions app/Livewire/Traits/LabelTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Livewire\Traits;

use App\Livewire\QrCodeComponent;
use Da\QrCode\Contracts\LabelInterface;

trait LabelTrait
{
public string $label = '';
public int $size = 16;
public string $align;

public function applyLabel()
{
$this->validate($this->getLabelRules());

$this->dispatch('apply-label', $this->label, $this->size, $this->align)
->to(QrCodeComponent::class);
}

public function getLabelRules(): array
{
return [
'label' => 'string|required',
'align' => 'string|required',
'size' => 'int|required|min:1'
];
}
}
23 changes: 21 additions & 2 deletions app/Livewire/Traits/LogoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@

namespace App\Livewire\Traits;

use App\Livewire\QrCodeComponent;
use Illuminate\Support\Facades\Storage;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use Livewire\WithFileUploads;

trait LogoTrait
{
public string $path = '';
use WithFileUploads;

public $file;

public function applyLogo()
{
$this->validate($this->getLogoRules());
/** @var TemporaryUploadedFile $filename */
$filename = uniqid() . '.' . $this->file->getClientOriginalExtension();
#$filename->storePubliclyAs('public/qrcode', $filename);
$this->file->storePubliclyAs('public/qrcode', $filename);

$this->dispatch('apply-logo', '../storage/app/public/qrcode/' . $filename, 'local')
->to(QrCodeComponent::class);
}

public function getLogoRules()
{
return [
'path' => 'string|nullable|regex:(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*))"'
'file' => 'image|required|max:1024',
];
}
}
25 changes: 25 additions & 0 deletions app/Livewire/Traits/MarginTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Livewire\Traits;

use App\Livewire\QrCodeComponent;

trait MarginTrait
{
public int $margins = 15;

public function applyMargin()
{
$this->validate($this->getMarginRules());

$this->dispatch('apply-margin', $this->margins)
->to(QrCodeComponent::class);
}

public function getMarginRules(): array
{
return [
'margins' => 'int|required|min:0',
];
}
}
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
"postcss": "^8.4.28",
"tailwindcss": "^3.3.3",
"vite": "^4.0.0"
},
"dependencies": {
"cash-dom": "^8.1.5",
"flatpickr": "^4.6.13"
}
}
Loading

0 comments on commit 2dc44ff

Please sign in to comment.