Skip to content

Commit 1f3e1e9

Browse files
weiting-sflxDASPRiD
authored andcommitted
feat: make utf-8 eci prefix configurable (#130)
1 parent c6f79a4 commit 1f3e1e9

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
FROM php:8.1-cli
22

33
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
4-
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
5-
RUN pecl install imagick && docker-php-ext-enable imagick
4+
RUN apt-get update && apt-get install -y libmagickwand-dev libzip-dev zip --no-install-recommends && rm -rf /var/lib/apt/lists/*
5+
RUN pecl install imagick && docker-php-ext-enable imagick && docker-php-ext-install zip
66
RUN alias composer='php /usr/bin/composer'
77

88
WORKDIR /app

src/Encoder/Encoder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public static function encode(
4848
string $content,
4949
ErrorCorrectionLevel $ecLevel,
5050
string $encoding = self::DEFAULT_BYTE_MODE_ECODING,
51-
?Version $forcedVersion = null
51+
?Version $forcedVersion = null,
52+
// Barcode scanner might not be able to read the encoded message of the QR code with the prefix ECI of UTF-8
53+
bool $prefixEci = true
5254
) : QrCode {
5355
// Pick an encoding mode appropriate for the content. Note that this
5456
// will not attempt to use multiple modes / segments even if that were
@@ -60,7 +62,7 @@ public static function encode(
6062
$headerBits = new BitArray();
6163

6264
// Append ECI segment if applicable
63-
if (Mode::BYTE() === $mode && self::DEFAULT_BYTE_MODE_ECODING !== $encoding) {
65+
if ($prefixEci && Mode::BYTE() === $mode && self::DEFAULT_BYTE_MODE_ECODING !== $encoding) {
6466
$eci = CharacterSetEci::getCharacterSetEciByName($encoding);
6567

6668
if (null !== $eci) {

test/Encoder/EncoderTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,41 @@ public function testSimpleUtf8Eci() : void
160160
$this->assertSame($expected, (string) $qrCode);
161161
}
162162

163+
public function testSimpleUtf8WithoutEci() : void
164+
{
165+
$qrCode = Encoder::encode('hello', ErrorCorrectionLevel::H(), 'utf-8', null, false);
166+
$expected = "<<\n"
167+
. " mode: BYTE\n"
168+
. " ecLevel: H\n"
169+
. " version: 1\n"
170+
. " maskPattern: 6\n"
171+
. " matrix:\n"
172+
. " 1 1 1 1 1 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 1\n"
173+
. " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n"
174+
. " 1 0 1 1 1 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 1\n"
175+
. " 1 0 1 1 1 0 1 0 1 0 1 0 0 0 1 0 1 1 1 0 1\n"
176+
. " 1 0 1 1 1 0 1 0 0 0 1 1 1 0 1 0 1 1 1 0 1\n"
177+
. " 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1\n"
178+
. " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n"
179+
. " 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0\n"
180+
. " 0 0 0 1 1 0 1 1 0 1 1 1 0 0 0 0 0 1 1 0 0\n"
181+
. " 1 0 1 0 0 1 0 1 0 1 1 0 0 0 1 1 1 1 1 0 0\n"
182+
. " 1 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1\n"
183+
. " 1 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 1 0 1 0 0\n"
184+
. " 0 1 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 0 1 0\n"
185+
. " 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 0 0 1 0 1 0\n"
186+
. " 1 1 1 1 1 1 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0\n"
187+
. " 1 0 0 0 0 0 1 0 0 1 1 1 0 1 0 0 0 1 1 1 1\n"
188+
. " 1 0 1 1 1 0 1 0 1 0 1 0 1 0 0 0 1 1 0 1 1\n"
189+
. " 1 0 1 1 1 0 1 0 1 0 1 1 1 1 0 0 1 0 0 0 0\n"
190+
. " 1 0 1 1 1 0 1 0 0 0 1 1 0 1 1 1 1 1 1 1 1\n"
191+
. " 1 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1\n"
192+
. " 1 1 1 1 1 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0\n"
193+
. ">>\n";
194+
195+
$this->assertSame($expected, (string) $qrCode);
196+
}
197+
163198
public function testAppendModeInfo() : void
164199
{
165200
$bits = new BitArray();

0 commit comments

Comments
 (0)