Skip to content

Commit 091cdaf

Browse files
committed
Add logo to EpsWriter
--- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/endroid/qr-code?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 6360c79 commit 091cdaf

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Writer/EpsWriter.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,43 @@ public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?Lab
3939
}
4040
}
4141

42+
if ($logo instanceof LogoInterface) {
43+
$this->addLogo($logo, $lines, $matrix->getOuterSize());
44+
}
45+
4246
return new EpsResult($matrix, $lines);
4347
}
48+
49+
private function addLogo(LogoInterface $logo, array &$lines, int $outerSize): void
50+
{
51+
$logoPath = $logo->getPath();
52+
$logoHeight = $logo->getResizeToHeight();
53+
$logoWidth = $logo->getResizeToWidth();
54+
55+
if (null === $logoHeight || null === $logoWidth) {
56+
$imageSize = \getimagesize($logoPath);
57+
if (!$imageSize) {
58+
throw new \Exception(sprintf('Unable to read image size for logo "%s"', $logoPath));
59+
}
60+
[$logoSourceWidth, $logoSourceHeight] = $imageSize;
61+
62+
if (null === $logoWidth) {
63+
$logoWidth = (int) $logoSourceWidth;
64+
}
65+
66+
if (null === $logoHeight) {
67+
$aspectRatio = $logoWidth / $logoSourceWidth;
68+
$logoHeight = (int) ($logoSourceHeight * $aspectRatio);
69+
}
70+
}
71+
72+
$logoX = $outerSize / 2 - $logoWidth / 2;
73+
$logoY = $outerSize / 2 - $logoHeight / 2;
74+
75+
$lines[] = 'gsave';
76+
$lines[] = number_format($logoX, self::DECIMAL_PRECISION, '.', '').' '.number_format($logoY, self::DECIMAL_PRECISION, '.', '').' translate';
77+
$lines[] = number_format($logoWidth, self::DECIMAL_PRECISION, '.', '').' '.number_format($logoHeight, self::DECIMAL_PRECISION, '.', '').' scale';
78+
$lines[] = '('.$logoPath.') run';
79+
$lines[] = 'grestore';
80+
}
4481
}

tests/Writer/EpsWriterTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Endroid\QrCode\Tests\Writer;
6+
7+
use Endroid\QrCode\QrCode;
8+
use Endroid\QrCode\Writer\EpsWriter;
9+
use Endroid\QrCode\Logo\Logo;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class EpsWriterTest extends TestCase
13+
{
14+
public function testWriteWithLogo(): void
15+
{
16+
$qrCode = new QrCode('QR Code with logo');
17+
$logo = new Logo(__DIR__.'/../assets/symfony.svg', 50, 50);
18+
19+
$writer = new EpsWriter();
20+
$result = $writer->write($qrCode, $logo);
21+
22+
$this->assertStringContainsString('gsave', $result->getString());
23+
$this->assertStringContainsString('translate', $result->getString());
24+
$this->assertStringContainsString('scale', $result->getString());
25+
$this->assertStringContainsString('run', $result->getString());
26+
}
27+
}

0 commit comments

Comments
 (0)