Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Writer/Png1bppWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\Png1bppResult;
use Endroid\QrCode\Writer\Result\ResultInterface;

final readonly class Png1bppWriter extends AbstractGdWriter
{
public const WRITER_OPTION_COMPRESSION_LEVEL = 'compression_level';

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;
}

/** @var GdResult $gdResult */
$gdResult = parent::write($qrCode, $logo, $label, $options);

return new Png1bppResult($gdResult->getMatrix(), $gdResult->getImage(), $options[self::WRITER_OPTION_COMPRESSION_LEVEL]);
}
}
32 changes: 32 additions & 0 deletions src/Writer/Result/Png1bppResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Endroid\QrCode\Writer\Result;

use Endroid\QrCode\Matrix\MatrixInterface;

final class Png1bppResult extends GdResult
{
public function __construct(
MatrixInterface $matrix,
\GdImage $image,
private readonly int $quality = -1,
) {
parent::__construct($matrix, $image);
}

public function getString(): string
{
ob_start();
imagetruecolortopalette($this->image, false, 2);
imagepng($this->image, quality: $this->quality);

return strval(ob_get_clean());
}

public function getMimeType(): string
{
return 'image/png';
}
}
Loading