Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Feb 23, 2023
1 parent 1cea1e0 commit ff1f769
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 143 deletions.
30 changes: 4 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
use Endroid\QrCode\Label\Font\NotoSans;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\GdWriter;
use Endroid\QrCode\Writer\Result\PngResult;
use Endroid\QrCode\Writer\PngWriter;

$result = Builder::create()
->writer(new GdWriter())
->writer(new PngWriter())
->writerOptions([])
->data('Custom QR code contents')
->encoding(new Encoding('UTF-8'))
Expand All @@ -66,10 +65,10 @@ use Endroid\QrCode\QrCode;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\GdWriter;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\ValidationException;

$writer = new GdWriter();
$writer = new PngWriter();

// Create QR code
$qrCode = QrCode::create('Life is too short to be generating QR codes')
Expand Down Expand Up @@ -120,27 +119,6 @@ use Endroid\QrCode\Writer\SvgWriter;
$builder->setWriterOptions([SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true]);
```

```php
use Endroid\QrCode\Writer\GdWriter;
use Endroid\QrCode\Writer\Result\PngResult;

$builder->setWriterOptions([GdWriter::WRITER_OPTION_RESULT_CLASS => PngResult::class, PngResult::RESULT_OPTION_QUALITY => -1]);
```

```php
use Endroid\QrCode\Writer\GdWriter;
use Endroid\QrCode\Writer\Result\WebpResult;

$builder->setWriterOptions([GdWriter::WRITER_OPTION_RESULT_CLASS => WebpResult::class, WebpResult::RESULT_OPTION_QUALITY => 80]);
```

```php
use Endroid\QrCode\Writer\GdWriter;
use Endroid\QrCode\Writer\Result\GifResult;

$builder->setWriterOptions([GdWriter::WRITER_OPTION_RESULT_CLASS => GifResult::class]);
```

### Encoding

If you use a barcode scanner you can have some troubles while reading the
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface;
use Endroid\QrCode\Writer\GdWriter;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\Result\ResultInterface;
use Endroid\QrCode\Writer\ValidatingWriterInterface;
use Endroid\QrCode\Writer\WriterInterface;
Expand Down Expand Up @@ -57,7 +57,7 @@ public function __construct()
{
$this->options = [
'data' => '',
'writer' => new GdWriter(),
'writer' => new PngWriter(),
'writerOptions' => [],
'qrCodeClass' => QrCode::class,
'logoClass' => Logo::class,
Expand Down
5 changes: 0 additions & 5 deletions src/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public static function createForMissingPackage(string $packageName): self
return new self(sprintf('Please install "%s" or disable image validation', $packageName));
}

public static function createForIncompatiblePhpVersion(): self
{
return new self('The validator is not compatible with PHP 8 yet, see https://github.com/khanamiryan/php-qrcode-detector-decoder/pull/103');
}

public static function createForInvalidData(string $expectedData, string $actualData): self
{
return new self('The validation reader read "'.$actualData.'" instead of "'.$expectedData.'". Adjust your parameters to increase readability or disable validation.');
Expand Down
26 changes: 7 additions & 19 deletions src/Writer/GdWriter.php → src/Writer/AbstractGdWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,14 @@
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeNone;
use Endroid\QrCode\Writer\Result\AbstractGdResult;
use Endroid\QrCode\Writer\Result\PngResult;
use Endroid\QrCode\Writer\Result\GdResult;
use Endroid\QrCode\Writer\Result\ResultInterface;
use Zxing\QrReader;

class GdWriter implements WriterInterface, ValidatingWriterInterface
abstract class AbstractGdWriter implements WriterInterface, ValidatingWriterInterface
{
public const WRITER_OPTION_RESULT_CLASS = 'result_class';
private string $resultClass = PngResult::class;

public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface
{
if (isset($options[self::WRITER_OPTION_RESULT_CLASS])) {
$this->resultClass = $options[self::WRITER_OPTION_RESULT_CLASS];

if (!is_subclass_of($this->resultClass, AbstractGdResult::class)) {
throw new \Exception($this->resultClass . ' must extend ' . AbstractGdResult::class);
}
}

if (!extension_loaded('gd')) {
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly');
}
Expand Down Expand Up @@ -119,7 +107,7 @@ public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null,
imagesavealpha($targetImage, true);
}

$result = new $this->resultClass($matrix, $targetImage, $options);
$result = new GdResult($matrix, $targetImage);

if ($logo instanceof LogoInterface) {
$result = $this->addLogo($logo, $result);
Expand All @@ -132,7 +120,7 @@ public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null,
return $result;
}

private function addLogo(LogoInterface $logo, AbstractGdResult $result): AbstractGdResult
private static function addLogo(LogoInterface $logo, GdResult $result): GdResult
{
$logoImageData = LogoImageData::createForLogo($logo);

Expand Down Expand Up @@ -169,10 +157,10 @@ private function addLogo(LogoInterface $logo, AbstractGdResult $result): Abstrac
imagesy($logoImageData->getImage())
);

return new $this->resultClass($matrix, $targetImage);
return new GdResult($matrix, $targetImage);
}

private function addLabel(LabelInterface $label, AbstractGdResult $result): AbstractGdResult
private static function addLabel(LabelInterface $label, GdResult $result): GdResult
{
$targetImage = $result->getImage();

Expand All @@ -198,7 +186,7 @@ private function addLabel(LabelInterface $label, AbstractGdResult $result): Abst

imagettftext($targetImage, $label->getFont()->getSize(), 0, $x, $y, $textColor, $label->getFont()->getPath(), $label->getText());

return new $this->resultClass($result->getMatrix(), $targetImage);
return new GdResult($result->getMatrix(), $targetImage);
}

public function validateResult(ResultInterface $result, string $expectedData): void
Expand Down
23 changes: 23 additions & 0 deletions src/Writer/GifWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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\GifResult;
use Endroid\QrCode\Writer\Result\ResultInterface;

final class GifWriter extends AbstractGdWriter
{
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface
{
/** @var GdResult $gdResult */
$gdResult = parent::write($qrCode, $logo, $label, $options);

return new GifResult($gdResult->getMatrix(), $gdResult->getImage());
}
}
25 changes: 21 additions & 4 deletions src/Writer/PngWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@

namespace Endroid\QrCode\Writer;

/**
* @deprecated Use Endroid\QrCode\GdWriter
*/
class PngWriter extends GdWriter
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 class PngWriter extends AbstractGdWriter
{
public const WRITER_OPTION_QUALITY = 'quality';

public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface
{
if (!isset($options[self::WRITER_OPTION_QUALITY])) {
$options[self::WRITER_OPTION_QUALITY] = -1;
}

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

return new PngResult($gdResult->getMatrix(), $gdResult->getImage(), $options[self::WRITER_OPTION_QUALITY]);
}
}
29 changes: 0 additions & 29 deletions src/Writer/Result/AbstractGdResult.php

This file was deleted.

32 changes: 32 additions & 0 deletions src/Writer/Result/GdResult.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;

class GdResult extends AbstractResult
{
public function __construct(
MatrixInterface $matrix,
protected \GdImage $image
) {
parent::__construct($matrix);
}

public function getImage(): \GdImage
{
return $this->image;
}

public function getString(): string
{
throw new \Exception('You can only use this method in a concrete implementation');
}

public function getMimeType(): string
{
throw new \Exception('You can only use this method in a concrete implementation');
}
}
4 changes: 2 additions & 2 deletions src/Writer/Result/GifResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Endroid\QrCode\Writer\Result;

final class GifResult extends AbstractGdResult
final class GifResult extends GdResult
{
public function getString(): string
{
ob_start();
imagegif($this->image);

return (string) ob_get_clean();
return strval(ob_get_clean());
}

public function getMimeType(): string
Expand Down
17 changes: 9 additions & 8 deletions src/Writer/Result/PngResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

namespace Endroid\QrCode\Writer\Result;

final class PngResult extends AbstractGdResult
use Endroid\QrCode\Matrix\MatrixInterface;

final class PngResult extends GdResult
{
public const RESULT_OPTION_QUALITY = 'result_quality';
private int $quality;

protected function initOptions(): void
public function __construct(MatrixInterface $matrix, \GdImage $image, int $quality = -1)
{
if (!isset($this->options[self::RESULT_OPTION_QUALITY])) {
$this->options[self::RESULT_OPTION_QUALITY] = -1;
}
parent::__construct($matrix, $image);
$this->quality = $quality;
}

public function getString(): string
{
ob_start();
imagepng($this->image, quality: $this->options[self::RESULT_OPTION_QUALITY]);
imagepng($this->image, quality: $this->quality);

return (string) ob_get_clean();
return strval(ob_get_clean());
}

public function getMimeType(): string
Expand Down
35 changes: 35 additions & 0 deletions src/Writer/Result/WebPResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Endroid\QrCode\Writer\Result;

use Endroid\QrCode\Matrix\MatrixInterface;

final class WebPResult extends GdResult
{
private int $quality;

public function __construct(MatrixInterface $matrix, \GdImage $image, int $quality = -1)
{
parent::__construct($matrix, $image);
$this->quality = $quality;
}

public function getString(): string
{
if (!function_exists('imagewebp')) {
throw new \Exception('WebP support is not available in your GD installation');
}

ob_start();
imagewebp($this->image, quality: $this->quality);

return strval(ob_get_clean());
}

public function getMimeType(): string
{
return 'image/webp';
}
}
30 changes: 0 additions & 30 deletions src/Writer/Result/WebpResult.php

This file was deleted.

Loading

0 comments on commit ff1f769

Please sign in to comment.