Skip to content

Commit

Permalink
fix qr generator for SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhsan3adi committed Jul 27, 2024
1 parent c036027 commit ed032bd
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions app/Controllers/Admin/QRGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\SvgWriter;
use Endroid\QrCode\Writer\WriterInterface;

class QRGenerator extends BaseController
{
protected QrCode $qrCode;
protected PngWriter $writer;
protected ?Logo $logo;
protected WriterInterface $writer;
protected ?Logo $logo = null;
protected Label $label;
protected Font $labelFont;
protected Color $foregroundColor;
Expand All @@ -29,25 +31,39 @@ class QRGenerator extends BaseController

protected string $qrCodeFilePath;

const PUBLIC_PATH = ROOTPATH . 'public' . DIRECTORY_SEPARATOR;
const UPLOADS_PATH = self::PUBLIC_PATH . 'uploads' . DIRECTORY_SEPARATOR;
const UPLOADS_PATH = FCPATH . 'uploads' . DIRECTORY_SEPARATOR;

public function __construct()
{
$this->setQrCodeFilePath(self::UPLOADS_PATH);

$this->writer = new PngWriter();

$this->labelFont = new Font(self::PUBLIC_PATH . 'assets/fonts/Roboto-Medium.ttf', 14);
$this->labelFont = new Font(FCPATH . 'assets/fonts/Roboto-Medium.ttf', 14);

$this->foregroundColor = new Color(44, 73, 162);
$this->foregroundColor2 = new Color(28, 101, 90);
$this->backgroundColor = new Color(255, 255, 255);

// Create logo
$this->logo = boolval(env('QR_LOGO'))
? Logo::create(self::PUBLIC_PATH . 'assets/img/logo_sekolah.jpg')->setResizeToWidth(75)
: null;
if (boolval(env('QR_LOGO'))) {
// Create logo
$logo = (new \Config\School)::$generalSettings->logo;
if (empty($logo) || !file_exists(FCPATH . $logo)) {
$logo = 'assets/img/logo_sekolah.jpg';
}
if (file_exists(FCPATH . $logo)) {
$fileExtension = pathinfo(FCPATH . $logo, PATHINFO_EXTENSION);
if ($fileExtension === 'svg') {
$this->writer = new SvgWriter();
$this->logo = Logo::create(FCPATH . $logo)
->setResizeToWidth(75)
->setResizeToHeight(75);
} else {
$this->logo = Logo::create(FCPATH . $logo)
->setResizeToWidth(75);
}
}
}

$this->label = Label::create('')
->setFont($this->labelFont)
Expand Down Expand Up @@ -114,7 +130,8 @@ public function generateQrGuru()

public function generate($nama, $nomor, $unique_code)
{
$filename = url_title($nama, lowercase: true) . "_" . url_title($nomor, lowercase: true) . '.png';
$fileExt = $this->writer instanceof SvgWriter ? 'svg' : 'png';
$filename = url_title($nama, lowercase: true) . "_" . url_title($nomor, lowercase: true) . ".$fileExt";

// set qr code data
$this->qrCode->setData($unique_code);
Expand Down

0 comments on commit ed032bd

Please sign in to comment.