Skip to content

Commit 74b62f4

Browse files
committed
Optimize color use in PNG writer
1 parent 6360c79 commit 74b62f4

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2024 (c) Jeroen van den Enden
1+
Copyright 2025 (c) Jeroen van den Enden
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ found as a constant prefixed with WRITER_OPTION_ in the writer class.
137137
* `link`: a URL or an identifier returned by `AddLink()`.
138138
* `PngWriter`
139139
* `compression_level`: compression level (0-9, default: -1 = zlib default)
140+
* `number_of_colors`: number of colors (1-256, null for true color)
140141
* `SvgWriter`
141142
* `block_id`: id of the block element for external reference (default: block)
142143
* `exclude_xml_declaration`: exclude XML declaration (default: false)

src/Writer/PngWriter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,27 @@
1414
final readonly class PngWriter extends AbstractGdWriter
1515
{
1616
public const WRITER_OPTION_COMPRESSION_LEVEL = 'compression_level';
17+
public const WRITER_OPTION_NUMBER_OF_COLORS = 'number_of_colors';
1718

1819
public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?LabelInterface $label = null, array $options = []): ResultInterface
1920
{
2021
if (!isset($options[self::WRITER_OPTION_COMPRESSION_LEVEL])) {
2122
$options[self::WRITER_OPTION_COMPRESSION_LEVEL] = -1;
2223
}
2324

25+
if (!isset($options[self::WRITER_OPTION_NUMBER_OF_COLORS])) {
26+
// When a logo is present use true color, otherwise use a palette of 16 colors
27+
$options[self::WRITER_OPTION_NUMBER_OF_COLORS] = $logo instanceof LogoInterface ? null : 16;
28+
}
29+
2430
/** @var GdResult $gdResult */
2531
$gdResult = parent::write($qrCode, $logo, $label, $options);
2632

27-
return new PngResult($gdResult->getMatrix(), $gdResult->getImage(), $options[self::WRITER_OPTION_COMPRESSION_LEVEL]);
33+
return new PngResult(
34+
$gdResult->getMatrix(),
35+
$gdResult->getImage(),
36+
$options[self::WRITER_OPTION_COMPRESSION_LEVEL],
37+
$options[self::WRITER_OPTION_NUMBER_OF_COLORS]
38+
);
2839
}
2940
}

src/Writer/Result/PngResult.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ public function __construct(
1212
MatrixInterface $matrix,
1313
\GdImage $image,
1414
private readonly int $quality = -1,
15+
private readonly ?int $numberOfColors = null,
1516
) {
1617
parent::__construct($matrix, $image);
1718
}
1819

1920
public function getString(): string
2021
{
2122
ob_start();
23+
if (null !== $this->numberOfColors) {
24+
imagetruecolortopalette($this->image, false, $this->numberOfColors);
25+
}
2226
imagepng($this->image, quality: $this->quality);
2327

2428
return strval(ob_get_clean());

0 commit comments

Comments
 (0)