2021-09-07 23:01:53 +02:00
|
|
|
<?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;
|
2021-09-07 23:01:53 +02:00
|
|
|
|
|
|
|
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
|
2021-09-07 23:01:53 +02:00
|
|
|
*/
|
2022-07-30 10:46:02 +02:00
|
|
|
public static function encode(string $data)
|
2021-09-07 23:01:53 +02:00
|
|
|
{
|
|
|
|
$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');
|
|
|
|
|
2021-09-07 23:01:53 +02:00
|
|
|
return $qrcode->render($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode an uploaded QR code image
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2022-08-26 15:57:18 +02:00
|
|
|
* @return string
|
2021-09-07 23:01:53 +02:00
|
|
|
*/
|
2022-07-30 10:46:02 +02:00
|
|
|
public static function decode(\Illuminate\Http\UploadedFile $file)
|
2021-09-07 23:01:53 +02:00
|
|
|
{
|
|
|
|
$qrcode = new QrReader($file->get(), QrReader::SOURCE_TYPE_BLOB);
|
2022-11-22 15:15:52 +01:00
|
|
|
$data = urldecode($qrcode->text());
|
2021-09-07 23:01:53 +02:00
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
if (! $data) {
|
2021-09-07 23:01:53 +02:00
|
|
|
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');
|
2021-09-07 23:01:53 +02:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|