2021-07-16 17:32:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Grocy\Controllers;
|
|
|
|
|
|
|
|
use Grocy\Helpers\Grocycode;
|
|
|
|
use jucksearm\barcode\lib\BarcodeFactory;
|
|
|
|
use jucksearm\barcode\lib\DatamatrixFactory;
|
2023-05-13 14:43:51 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
2021-07-16 17:32:08 +02:00
|
|
|
|
|
|
|
trait GrocycodeTrait
|
|
|
|
{
|
2023-05-13 14:43:51 +02:00
|
|
|
public function ServeGrocycodeImage(Request $request, Response $response, Grocycode $grocycode)
|
2021-07-16 17:32:08 +02:00
|
|
|
{
|
|
|
|
$size = $request->getQueryParam('size', null);
|
|
|
|
|
|
|
|
if (GROCY_GROCYCODE_TYPE == '2D')
|
|
|
|
{
|
|
|
|
$png = (new DatamatrixFactory())->setCode((string) $grocycode)->setSize($size)->getDatamatrixPngData();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$png = (new BarcodeFactory())->setType('C128')->setCode((string) $grocycode)->setHeight($size)->getBarcodePngData();
|
|
|
|
}
|
|
|
|
|
|
|
|
$isDownload = $request->getQueryParam('download', false);
|
|
|
|
if ($isDownload)
|
|
|
|
{
|
|
|
|
$response = $response->withHeader('Content-Type', 'application/octet-stream')
|
|
|
|
->withHeader('Content-Disposition', 'attachment; filename=grocycode.png')
|
|
|
|
->withHeader('Content-Length', strlen($png))
|
|
|
|
->withHeader('Cache-Control', 'no-cache')
|
|
|
|
->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$response = $response->withHeader('Content-Type', 'image/png')
|
|
|
|
->withHeader('Content-Length', strlen($png))
|
|
|
|
->withHeader('Cache-Control', 'no-cache')
|
|
|
|
->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
|
|
|
|
}
|
|
|
|
$response->getBody()->write($png);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|