2FAuth/app/Services/QrCodeService.php

52 lines
1.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
2022-11-22 15:15:52 +01:00
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
2021-10-15 23:46:21 +02:00
use Illuminate\Support\Facades\Log;
2022-11-22 15:15:52 +01:00
use Zxing\QrReader;
class QrCodeService
{
/**
* Encode a string into a QR code image
2022-11-22 15:15:52 +01:00
*
* @param string $data The string to encode
* @return mixed
*/
public static function encode(string $data)
{
$options = new QROptions([
'quietzoneSize' => 2,
'scale' => 8,
]);
$qrcode = new QRCode($options);
2021-10-15 23:46:21 +02:00
Log::info('data encoded to QR code');
return $qrcode->render($data);
}
/**
* Decode an uploaded QR code image
2022-11-22 15:15:52 +01:00
*
* @param \Illuminate\Http\UploadedFile $file
* @return string
*/
public static function decode(\Illuminate\Http\UploadedFile $file)
{
$qrcode = new QrReader($file->get(), QrReader::SOURCE_TYPE_BLOB);
2022-11-22 15:15:52 +01:00
$data = urldecode($qrcode->text());
2022-11-22 15:15:52 +01:00
if (! $data) {
throw new \App\Exceptions\InvalidQrCodeException;
}
2022-11-22 15:15:52 +01:00
2021-10-15 23:46:21 +02:00
Log::info('QR code decoded');
return $data;
}
2022-11-22 15:15:52 +01:00
}