-
-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathPngWriter.php
More file actions
44 lines (36 loc) · 1.56 KB
/
PngWriter.php
File metadata and controls
44 lines (36 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer;
use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\Writer\Result\GdResult;
use Endroid\QrCode\Writer\Result\PngResult;
use Endroid\QrCode\Writer\Result\ResultInterface;
final readonly class PngWriter extends AbstractGdWriter
{
public const string WRITER_OPTION_COMPRESSION_LEVEL = 'compression_level';
public const string WRITER_OPTION_NUMBER_OF_COLORS = 'number_of_colors';
#[\Override]
public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?LabelInterface $label = null, array $options = []): ResultInterface
{
if (!isset($options[self::WRITER_OPTION_COMPRESSION_LEVEL])) {
$options[self::WRITER_OPTION_COMPRESSION_LEVEL] = -1;
}
if (!array_key_exists(self::WRITER_OPTION_NUMBER_OF_COLORS, $options)) {
$options[self::WRITER_OPTION_NUMBER_OF_COLORS] = match (true) {
$qrCode->getBackgroundColor()->getAlpha() > 0 || $qrCode->getForegroundColor()->getAlpha() > 0 => null,
$logo instanceof LogoInterface => null,
default => 16,
};
}
/** @var GdResult $gdResult */
$gdResult = parent::write($qrCode, $logo, $label, $options);
return new PngResult(
$gdResult->getMatrix(),
$gdResult->getImage(),
$options[self::WRITER_OPTION_COMPRESSION_LEVEL],
$options[self::WRITER_OPTION_NUMBER_OF_COLORS]
);
}
}