Color
1 week ago
Eye
1 week ago
Image
1 week ago
Module
1 week ago
Path
1 week ago
RendererStyle
1 week ago
ImageRenderer.php
1 week ago
PlainTextRenderer.php
1 week ago
RendererInterface.php
1 week ago
index.php
1 week ago
ImageRenderer.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace WP2FA_Vendor\BaconQrCode\Renderer; |
| 5 | |
| 6 | use WP2FA_Vendor\BaconQrCode\Encoder\MatrixUtil; |
| 7 | use WP2FA_Vendor\BaconQrCode\Encoder\QrCode; |
| 8 | use WP2FA_Vendor\BaconQrCode\Exception\InvalidArgumentException; |
| 9 | use WP2FA_Vendor\BaconQrCode\Renderer\Image\ImageBackEndInterface; |
| 10 | use WP2FA_Vendor\BaconQrCode\Renderer\Path\Path; |
| 11 | use WP2FA_Vendor\BaconQrCode\Renderer\RendererStyle\EyeFill; |
| 12 | use WP2FA_Vendor\BaconQrCode\Renderer\RendererStyle\RendererStyle; |
| 13 | final class ImageRenderer implements RendererInterface |
| 14 | { |
| 15 | /** |
| 16 | * @var RendererStyle |
| 17 | */ |
| 18 | private $rendererStyle; |
| 19 | /** |
| 20 | * @var ImageBackEndInterface |
| 21 | */ |
| 22 | private $imageBackEnd; |
| 23 | public function __construct(RendererStyle $rendererStyle, ImageBackEndInterface $imageBackEnd) |
| 24 | { |
| 25 | $this->rendererStyle = $rendererStyle; |
| 26 | $this->imageBackEnd = $imageBackEnd; |
| 27 | } |
| 28 | /** |
| 29 | * @throws InvalidArgumentException if matrix width doesn't match height |
| 30 | */ |
| 31 | public function render(QrCode $qrCode) : string |
| 32 | { |
| 33 | $size = $this->rendererStyle->getSize(); |
| 34 | $margin = $this->rendererStyle->getMargin(); |
| 35 | $matrix = $qrCode->getMatrix(); |
| 36 | $matrixSize = $matrix->getWidth(); |
| 37 | if ($matrixSize !== $matrix->getHeight()) { |
| 38 | throw new InvalidArgumentException('Matrix must have the same width and height'); |
| 39 | } |
| 40 | $totalSize = $matrixSize + $margin * 2; |
| 41 | $moduleSize = $size / $totalSize; |
| 42 | $fill = $this->rendererStyle->getFill(); |
| 43 | $this->imageBackEnd->new($size, $fill->getBackgroundColor()); |
| 44 | $this->imageBackEnd->scale((float) $moduleSize); |
| 45 | $this->imageBackEnd->translate((float) $margin, (float) $margin); |
| 46 | $module = $this->rendererStyle->getModule(); |
| 47 | $moduleMatrix = clone $matrix; |
| 48 | MatrixUtil::removePositionDetectionPatterns($moduleMatrix); |
| 49 | $modulePath = $this->drawEyes($matrixSize, $module->createPath($moduleMatrix)); |
| 50 | if ($fill->hasGradientFill()) { |
| 51 | $this->imageBackEnd->drawPathWithGradient($modulePath, $fill->getForegroundGradient(), 0, 0, $matrixSize, $matrixSize); |
| 52 | } else { |
| 53 | $this->imageBackEnd->drawPathWithColor($modulePath, $fill->getForegroundColor()); |
| 54 | } |
| 55 | return $this->imageBackEnd->done(); |
| 56 | } |
| 57 | private function drawEyes(int $matrixSize, Path $modulePath) : Path |
| 58 | { |
| 59 | $fill = $this->rendererStyle->getFill(); |
| 60 | $eye = $this->rendererStyle->getEye(); |
| 61 | $externalPath = $eye->getExternalPath(); |
| 62 | $internalPath = $eye->getInternalPath(); |
| 63 | $modulePath = $this->drawEye($externalPath, $internalPath, $fill->getTopLeftEyeFill(), 3.5, 3.5, 0, $modulePath); |
| 64 | $modulePath = $this->drawEye($externalPath, $internalPath, $fill->getTopRightEyeFill(), $matrixSize - 3.5, 3.5, 90, $modulePath); |
| 65 | $modulePath = $this->drawEye($externalPath, $internalPath, $fill->getBottomLeftEyeFill(), 3.5, $matrixSize - 3.5, -90, $modulePath); |
| 66 | return $modulePath; |
| 67 | } |
| 68 | private function drawEye(Path $externalPath, Path $internalPath, EyeFill $fill, float $xTranslation, float $yTranslation, int $rotation, Path $modulePath) : Path |
| 69 | { |
| 70 | if ($fill->inheritsBothColors()) { |
| 71 | return $modulePath->append($externalPath->translate($xTranslation, $yTranslation))->append($internalPath->translate($xTranslation, $yTranslation)); |
| 72 | } |
| 73 | $this->imageBackEnd->push(); |
| 74 | $this->imageBackEnd->translate($xTranslation, $yTranslation); |
| 75 | if (0 !== $rotation) { |
| 76 | $this->imageBackEnd->rotate($rotation); |
| 77 | } |
| 78 | if ($fill->inheritsExternalColor()) { |
| 79 | $modulePath = $modulePath->append($externalPath->translate($xTranslation, $yTranslation)); |
| 80 | } else { |
| 81 | $this->imageBackEnd->drawPathWithColor($externalPath, $fill->getExternalColor()); |
| 82 | } |
| 83 | if ($fill->inheritsInternalColor()) { |
| 84 | $modulePath = $modulePath->append($internalPath->translate($xTranslation, $yTranslation)); |
| 85 | } else { |
| 86 | $this->imageBackEnd->drawPathWithColor($internalPath, $fill->getInternalColor()); |
| 87 | } |
| 88 | $this->imageBackEnd->pop(); |
| 89 | return $modulePath; |
| 90 | } |
| 91 | } |
| 92 |