mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 12:11:19 +03:00
Merge pull request #841 from acelaya-forks/feature/svg-qr-codes
Feature/svg qr codes
This commit is contained in:
commit
8d438aa6aa
4 changed files with 52 additions and 1 deletions
|
@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
|
|||
|
||||
#### Added
|
||||
|
||||
* *Nothing*
|
||||
* [#829](https://github.com/shlinkio/shlink/issues/829) Added support for QR codes in SVG format, by passing `?format=svg` to the QR code URL.
|
||||
|
||||
#### Changed
|
||||
|
||||
|
|
|
@ -27,6 +27,19 @@
|
|||
"maximum": 1000,
|
||||
"default": 300
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "format",
|
||||
"in": "query",
|
||||
"description": "The format for the QR code image, being valid values png and svg. Not providing the param or providing any other value will fall back to png.",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"png",
|
||||
"svg"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
|
@ -38,6 +51,12 @@
|
|||
"type": "string",
|
||||
"format": "binary"
|
||||
}
|
||||
},
|
||||
"image/svg+xml": {
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"format": "binary"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Shlinkio\Shlink\Core\Action;
|
||||
|
||||
use Endroid\QrCode\QrCode;
|
||||
use Endroid\QrCode\Writer\SvgWriter;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
|
@ -51,6 +52,11 @@ class QrCodeAction implements MiddlewareInterface
|
|||
$qrCode->setSize($this->getSizeParam($request));
|
||||
$qrCode->setMargin(0);
|
||||
|
||||
$format = $request->getQueryParams()['format'] ?? 'png';
|
||||
if ($format === 'svg') {
|
||||
$qrCode->setWriter(new SvgWriter());
|
||||
}
|
||||
|
||||
return new QrCodeResponse($qrCode);
|
||||
}
|
||||
|
||||
|
|
|
@ -81,4 +81,30 @@ class QrCodeActionTest extends TestCase
|
|||
$this->assertEquals(200, $resp->getStatusCode());
|
||||
$delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideQueries
|
||||
*/
|
||||
public function imageIsReturnedWithExpectedContentTypeBasedOnProvidedFormat(
|
||||
array $query,
|
||||
string $expectedContentType
|
||||
): void {
|
||||
$code = 'abc123';
|
||||
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($code, ''))->willReturn(new ShortUrl(''));
|
||||
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
||||
$req = (new ServerRequest())->withAttribute('shortCode', $code)->withQueryParams($query);
|
||||
|
||||
$resp = $this->action->process($req, $delegate->reveal());
|
||||
|
||||
$this->assertEquals($expectedContentType, $resp->getHeaderLine('Content-Type'));
|
||||
}
|
||||
|
||||
public function provideQueries(): iterable
|
||||
{
|
||||
yield 'no format' => [[], 'image/png'];
|
||||
yield 'png format' => [['format' => 'png'], 'image/png'];
|
||||
yield 'svg format' => [['format' => 'svg'], 'image/svg+xml'];
|
||||
yield 'unsupported format' => [['format' => 'jpg'], 'image/png'];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue