Skip to content

Commit 6fa27c4

Browse files
committed
New major
1 parent bea3bc8 commit 6fa27c4

File tree

85 files changed

+2425
-1919
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2425
-1919
lines changed

.github/workflows/CI.yml

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ jobs:
5454
vendor/bin/unit-test
5555
vendor/bin/functional-test ^3.4
5656
57+
- name: Archive logs
58+
if: ${{ failure() }}
59+
uses: actions/upload-artifact@v2
60+
with:
61+
name: logs
62+
path: vendor/endroid/quality/application/var/log
63+
5764
- name: Archive code coverage results
5865
uses: actions/upload-artifact@v2
5966
with:

README.md

+93-41
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ to generate the matrix and [khanamiryan/qrcode-detector-decoder](https://github.
1313
for validating generated QR codes. Further extended with Twig extensions, generation routes, a factory and a
1414
Symfony bundle for easy installation and configuration.
1515

16-
Different writers are provided to generate the QR code as PNG, SVG, EPS, PDF or in binary format.
16+
Different writers are provided to generate the QR code as PNG, SVG, EPS or in binary format.
1717

1818
## Installation
1919

@@ -23,49 +23,75 @@ Use [Composer](https://getcomposer.org/) to install the library.
2323
$ composer require endroid/qr-code
2424
```
2525

26-
## Basic usage
26+
## Usage: without using builder
2727

2828
```php
29+
use Endroid\QrCode\Color\Color;
30+
use Endroid\QrCode\Encoding\Encoding;
31+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
2932
use Endroid\QrCode\QrCode;
33+
use Endroid\QrCode\Label\Label;
34+
use Endroid\QrCode\Logo\Logo;
35+
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
36+
use Endroid\QrCode\Writer\PngWriter;
37+
38+
$writer = new PngWriter();
39+
40+
// Create QR code
41+
$qrCode = QrCode::create('Data')
42+
->setEncoding(new Encoding('UTF-8'))
43+
->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
44+
->setSize(300)
45+
->setMargin(10)
46+
->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
47+
->setForegroundColor(new Color(0, 0, 0))
48+
->setBackgroundColor(new Color(255, 255, 255));
49+
50+
// Create generic logo
51+
$logo = Logo::create(__DIR__.'/assets/symfony.png')
52+
->setResizeToWidth(50);
53+
54+
// Create generic label
55+
$label = Label::create('Label')
56+
->setTextColor(new Color(255, 0, 0))
57+
->setBackgroundColor(new Color(0, 0, 0));
58+
59+
$result = $writer->write($qrCode, $logo, $label);
60+
```
3061

31-
$qrCode = new QrCode('Life is too short to be generating QR codes');
62+
## Usage: using the builder
3263

33-
header('Content-Type: '.$qrCode->getContentType());
34-
echo $qrCode->writeString();
64+
```php
65+
use Endroid\QrCode\Builder\Builder;
66+
use Endroid\QrCode\Encoding\Encoding;
67+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
68+
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
69+
use Endroid\QrCode\Label\Font\NotoSans;
70+
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
71+
use Endroid\QrCode\Writer\PngWriter;
72+
73+
$result = Builder::create()
74+
->writer(new PngWriter())
75+
->writerOptions([])
76+
->data('Custom QR code contents')
77+
->encoding(new Encoding('UTF-8'))
78+
->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
79+
->size(300)
80+
->margin(10)
81+
->roundBlockSizeMode(new RoundBlockSizeModeMargin())
82+
->logoPath(__DIR__.'/assets/symfony.png')
83+
->labelText('This is the label')
84+
->labelFont(new NotoSans(20))
85+
->labelAlignment(new LabelAlignmentCenter())
86+
->build();
3587
```
3688

37-
## Advanced usage
89+
## Usage: working with results
3890

3991
```php
40-
use Endroid\QrCode\ErrorCorrectionLevel;
41-
use Endroid\QrCode\LabelAlignment;
42-
use Endroid\QrCode\QrCode;
43-
use Endroid\QrCode\Response\QrCodeResponse;
44-
45-
// Create a basic QR code
46-
$qrCode = new QrCode('Life is too short to be generating QR codes');
47-
$qrCode->setSize(300);
48-
$qrCode->setMargin(10);
49-
50-
// Set advanced options
51-
$qrCode->setWriterByName('png');
52-
$qrCode->setEncoding('UTF-8');
53-
$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
54-
$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
55-
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
56-
$qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER());
57-
$qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
58-
$qrCode->setLogoSize(150, 200);
59-
$qrCode->setValidateResult(false);
60-
61-
// Round block sizes to improve readability and make the blocks sharper in pixel based outputs (like png).
62-
// There are three approaches:
63-
$qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_MARGIN); // The size of the qr code is shrinked, if necessary, but the size of the final image remains unchanged due to additional margin being added (default)
64-
$qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_ENLARGE); // The size of the qr code and the final image is enlarged, if necessary
65-
$qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_SHRINK); // The size of the qr code and the final image is shrinked, if necessary
6692

6793
// Set additional writer options (SvgWriter example)
68-
$qrCode->setWriterOptions(['exclude_xml_declaration' => true]);
94+
$qrCode->
6995

7096
// Directly output the QR code
7197
header('Content-Type: '.$qrCode->getContentType());
@@ -80,12 +106,37 @@ $dataUri = $qrCode->writeDataUri();
80106

81107
![QR Code](https://endroid.nl/qr-code/Life%20is%20too%20short%20to%20be%20generating%20QR%20codes.png)
82108

109+
### Writer options
110+
111+
```php
112+
use Endroid\QrCode\Writer\SvgWriter;
113+
114+
$builder->setWriterOptions([SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true]);
115+
```
116+
83117
### Encoding
84-
You can pick one of these values for encoding:
85118

86-
`ISO-8859-1`, `ISO-8859-2`, `ISO-8859-3`, `ISO-8859-4`, `ISO-8859-5`, `ISO-8859-6`, `ISO-8859-7`, `ISO-8859-8`, `ISO-8859-9`, `ISO-8859-10`, `ISO-8859-11`, `ISO-8859-12`, `ISO-8859-13`, `ISO-8859-14`, `ISO-8859-15`, `ISO-8859-16`, `Shift_JIS`, `windows-1250`, `windows-1251`, `windows-1252`, `windows-1256`, `UTF-16BE`, `UTF-8`, `US-ASCII`, `GBK` `EUC-KR`
119+
If you use a barcode scanner you can have some troubles while reading the
120+
generated QR codes. Depending on the encoding you chose you will have an extra
121+
amount of data corresponding to the ECI block. Some barcode scanner are not
122+
programmed to interpret this block of information. To ensure a maximum
123+
compatibility you can use the `ISO-8859-1` encoding that is the default
124+
encoding used by barcode scanners (if your character set supports it,
125+
i.e. no Chinese characters are present).
87126

88-
If you use a barcode scanner you can have some troubles while reading the generated QR codes. Depending on the encoding you chose you will have an extra amount of data corresponding to the ECI block. Some barcode scanner are not programmed to interpret this block of information. For exemple the ECI block for `UTF-8` is `000026` so the above exemple will produce : `\000026Life is too short to be generating QR codes`. To ensure a maximum compatibility you can use the `ISO-8859-1` encoding that is the default encoding used by barcode scanners.
127+
### Round block size mode
128+
129+
By default block sizes are rounded to guarantee sharp images and improve
130+
readability. However some other rounding variants are available.
131+
132+
* RoundBlockSizeModeMargin (default): the size of the QR code is shrunk if
133+
necessary but the size of the final image remains unchanged due to additional
134+
margin being added.
135+
* RoundBlockSizeModeEnlarge: the size of the QR code and the final image are
136+
enlarged when rounding differences occur.
137+
* RoundBlockSizeModeShrink: the size of the QR code and the final image are
138+
shrunk when rounding differences occur.
139+
* RoundBlockSizeModeNone: No rounding.
89140

90141
## Readability
91142

@@ -95,24 +146,25 @@ can tweak these parameters if you are looking for optimal results. You can also
95146
check $qrCode->getRoundBlockSize() value to see if block dimensions are rounded
96147
so that the image is more sharp and readable. Please note that rounding block
97148
size can result in additional padding to compensate for the rounding difference.
149+
And finally the encoding (default UTF-8 to support large character sets) can be
150+
set to `ISO-8859-1` if possible to improve readability.
98151

99152
## Built-in validation reader
100153

101154
You can enable the built-in validation reader (disabled by default) by calling
102155
setValidateResult(true). This validation reader does not guarantee that the QR
103156
code will be readable by all readers but it helps you provide a minimum level
104-
of quality.
105-
106-
Take note that the validator can consume quite amount of additional resources.
157+
of quality. Take note that the validator can consume quite amount of additional
158+
resources and it should be installed separately only if you use it.
107159

108160
## Symfony integration
109161

110162
The [endroid/qr-code-bundle](https://github.com/endroid/qr-code-bundle)
111163
integrates the QR code library in Symfony for an even better experience.
112164

113165
* Configure your defaults (like image size, default writer etc.)
114-
* Generate QR codes quickly from anywhere via the factory service
115-
* Generate QR codes directly by typing an URL like /qr-code/\<text>.png?size=300
166+
* Support for multiple configurations and injection via aliases
167+
* Generate QR codes for defined configurations via URL like /qr-code/<config>/Hello
116168
* Generate QR codes or URLs directly from Twig using dedicated functions
117169

118170
Read the [bundle documentation](https://github.com/endroid/qr-code-bundle)
File renamed without changes.
File renamed without changes.

composer.json

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "endroid/qr-code",
33
"description": "Endroid QR Code",
4-
"keywords": ["endroid", "qrcode", "qr", "code", "bundle", "php"],
4+
"keywords": ["endroid", "qrcode", "qr", "code", "php"],
55
"homepage": "https://github.com/endroid/qr-code",
66
"type": "library",
77
"license": "MIT",
@@ -13,20 +13,19 @@
1313
],
1414
"require": {
1515
"php": "^7.3||^8.0",
16-
"bacon/bacon-qr-code": "^2.0",
17-
"khanamiryan/qrcode-detector-decoder": "^1.0.2",
18-
"myclabs/php-enum": "^1.5",
19-
"symfony/options-resolver": "^3.4||^4.4||^5.0",
20-
"symfony/property-access": "^3.4||^4.4||^5.0"
16+
"bacon/bacon-qr-code": "^2.0"
2117
},
2218
"require-dev": {
23-
"endroid/quality": "^1.5.2",
24-
"setasign/fpdf": "^1.8"
19+
"ext-gd": "*",
20+
"endroid/quality": "dev-master",
21+
"khanamiryan/qrcode-detector-decoder": "^1.0.4",
22+
"setasign/fpdf": "^1.8.2"
2523
},
2624
"suggest": {
27-
"ext-gd": "Required for generating PNG images",
28-
"roave/security-advisories": "Avoids installation of package versions with vulnerabilities",
29-
"setasign/fpdf": "Required to use the FPDF writer.",
25+
"ext-gd": "Enables you to write PNG images",
26+
"khanamiryan/qrcode-detector-decoder": "Enables you to use the image validator",
27+
"roave/security-advisories": "Makes sure package versions with known security issues are not installed",
28+
"setasign/fpdf": "Enables you to use the PDF writer.",
3029
"symfony/security-checker": "Checks your composer.lock for vulnerabilities"
3130
},
3231
"autoload": {
@@ -44,7 +43,7 @@
4443
},
4544
"extra": {
4645
"branch-alias": {
47-
"dev-master": "3.x-dev"
46+
"dev-master": "4.x-dev"
4847
}
4948
}
5049
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Endroid\QrCode\Bacon;
6+
7+
use BaconQrCode\Common\ErrorCorrectionLevel;
8+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
9+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface;
10+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
11+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelMedium;
12+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelQuartile;
13+
14+
final class ErrorCorrectionLevelConverter
15+
{
16+
public static function convertToBaconErrorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): ErrorCorrectionLevel
17+
{
18+
if ($errorCorrectionLevel instanceof ErrorCorrectionLevelLow) {
19+
return ErrorCorrectionLevel::valueOf('L');
20+
} elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelMedium) {
21+
return ErrorCorrectionLevel::valueOf('M');
22+
} elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelQuartile) {
23+
return ErrorCorrectionLevel::valueOf('Q');
24+
} elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelHigh) {
25+
return ErrorCorrectionLevel::valueOf('H');
26+
}
27+
28+
throw new \Exception('Error correction level could not be converted');
29+
}
30+
}

src/Bacon/MatrixFactory.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Endroid\QrCode\Bacon;
6+
7+
use BaconQrCode\Encoder\Encoder;
8+
use Endroid\QrCode\Matrix\Matrix;
9+
use Endroid\QrCode\Matrix\MatrixFactoryInterface;
10+
use Endroid\QrCode\Matrix\MatrixInterface;
11+
use Endroid\QrCode\QrCodeInterface;
12+
13+
final class MatrixFactory implements MatrixFactoryInterface
14+
{
15+
public function create(QrCodeInterface $qrCode): MatrixInterface
16+
{
17+
$baconErrorCorrectionLevel = ErrorCorrectionLevelConverter::convertToBaconErrorCorrectionLevel($qrCode->getErrorCorrectionLevel());
18+
$baconMatrix = Encoder::encode($qrCode->getData(), $baconErrorCorrectionLevel, strval($qrCode->getEncoding()))->getMatrix();
19+
20+
$blockValues = [];
21+
$columnCount = $baconMatrix->getWidth();
22+
$rowCount = $baconMatrix->getHeight();
23+
for ($rowIndex = 0; $rowIndex < $rowCount; ++$rowIndex) {
24+
$blockValues[$rowIndex] = [];
25+
for ($columnIndex = 0; $columnIndex < $columnCount; ++$columnIndex) {
26+
$blockValues[$rowIndex][$columnIndex] = $baconMatrix->get($columnIndex, $rowIndex);
27+
}
28+
}
29+
30+
return new Matrix($blockValues, $qrCode->getSize(), $qrCode->getMargin(), $qrCode->getRoundBlockSizeMode());
31+
}
32+
}

0 commit comments

Comments
 (0)