Skip to content

Commit c98adc4

Browse files
committed
UriBuilder: Add getQRCodeDataUri method
1 parent 35d58bb commit c98adc4

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/OtpAuth/UriBuilder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Vectorface\OtpAuth;
44

5+
use Endroid\QrCode\Builder\Builder;
6+
use Endroid\QrCode\Writer\PngWriter;
57
use Vectorface\OtpAuth\Paramters\Algorithm;
68
use Vectorface\OtpAuth\Paramters\Type;
79

@@ -136,4 +138,15 @@ public function __toString(): string
136138
{
137139
return $this->getUri();
138140
}
141+
142+
public function getQRCodeDataUri(): string
143+
{
144+
return Builder::create()
145+
->data($this->getUri())
146+
->writer(new PngWriter)
147+
->size(260)
148+
->margin(10)
149+
->build()
150+
->getDataUri();
151+
}
139152
}

tests/OtpAuth/UriBuilderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ public function synopsis()
1919
->account("MyAcct")
2020
->secret("FOO");
2121

22+
/* The builder can generate otpauth URLs */
2223
$this->assertEquals("otpauth://totp/MyAcct?secret=FOO", "$uriBuilder");
2324

25+
/* ... or QR codes as data URIs */
26+
$this->assertStringStartsWith("data:image/png;base64,", $uriBuilder->getQRCodeDataUri());
27+
2428
/* It is also possible to construct complex OTP URIs, including HOTP */
2529
$uriBuilder = (new UriBuilder())
2630
->type(Type::HOTP)

0 commit comments

Comments
 (0)