mirror of
https://github.com/shlinkio/shlink.git
synced 2026-10-12 21:31:58 -05:00
Added support to define the margin when generating the QR codes
This commit is contained in:
1 parent
b54350674c
commit
383fde488b
2 files changed
+35
-6
No files matched your search
@@ -50,12 +50,9 @@ class QrCodeAction implements MiddlewareInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
$query = $request->getQueryParams();
|
$query = $request->getQueryParams();
|
||||||
// Size attribute is deprecated
|
|
||||||
$size = $this->normalizeSize((int) $request->getAttribute('size', $query['size'] ?? self::DEFAULT_SIZE));
|
|
||||||
|
|
||||||
$qrCode = new QrCode($this->stringifier->stringify($shortUrl));
|
$qrCode = new QrCode($this->stringifier->stringify($shortUrl));
|
||||||
$qrCode->setSize($size);
|
$qrCode->setSize($this->resolveSize($request, $query));
|
||||||
$qrCode->setMargin(0);
|
$qrCode->setMargin($this->resolveMargin($query));
|
||||||
|
|
||||||
$format = $query['format'] ?? 'png';
|
$format = $query['format'] ?? 'png';
|
||||||
if ($format === 'svg') {
|
if ($format === 'svg') {
|
||||||
@@ -65,12 +62,29 @@ class QrCodeAction implements MiddlewareInterface
|
|||||||
return new QrCodeResponse($qrCode);
|
return new QrCodeResponse($qrCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function normalizeSize(int $size): int
|
private function resolveSize(Request $request, array $query): int
|
||||||
{
|
{
|
||||||
|
// Size attribute is deprecated. After v3.0.0, always use the query param instead
|
||||||
|
$size = (int) $request->getAttribute('size', $query['size'] ?? self::DEFAULT_SIZE);
|
||||||
if ($size < self::MIN_SIZE) {
|
if ($size < self::MIN_SIZE) {
|
||||||
return self::MIN_SIZE;
|
return self::MIN_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $size > self::MAX_SIZE ? self::MAX_SIZE : $size;
|
return $size > self::MAX_SIZE ? self::MAX_SIZE : $size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function resolveMargin(array $query): int
|
||||||
|
{
|
||||||
|
if (! isset($query['margin'])) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$margin = $query['margin'];
|
||||||
|
$intMargin = (int) $margin;
|
||||||
|
if ($margin !== (string) $intMargin) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $intMargin < 0 ? 0 : $intMargin;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -133,5 +133,20 @@ class QrCodeActionTest extends TestCase
|
|||||||
ServerRequestFactory::fromGlobals()->withAttribute('size', '350')->withQueryParams(['size' => '123']),
|
ServerRequestFactory::fromGlobals()->withAttribute('size', '350')->withQueryParams(['size' => '123']),
|
||||||
350,
|
350,
|
||||||
];
|
];
|
||||||
|
yield 'margin' => [ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']), 370];
|
||||||
|
yield 'margin and size' => [
|
||||||
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '100', 'size' => '200']),
|
||||||
|
400,
|
||||||
|
];
|
||||||
|
yield 'negative margin' => [ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-50']), 300];
|
||||||
|
yield 'non-numeric margin' => [ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo']), 300];
|
||||||
|
yield 'negative margin and size' => [
|
||||||
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-1', 'size' => '150']),
|
||||||
|
150,
|
||||||
|
];
|
||||||
|
yield 'non-numeric margin and size' => [
|
||||||
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo', 'size' => '538']),
|
||||||
|
538,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user