Debian Codegraph @

vendor/endroid/qr-code/README.md

raw
1 # QR Code
2
3 *By [endroid](https://endroid.nl/)*
4
5 [![Latest Stable Version](http://img.shields.io/packagist/v/endroid/qr-code.svg)](https://packagist.org/packages/endroid/qr-code)
6 [![Build Status](https://github.com/endroid/qr-code/workflows/CI/badge.svg)](https://github.com/endroid/qr-code/actions)
7 [![Total Downloads](http://img.shields.io/packagist/dt/endroid/qr-code.svg)](https://packagist.org/packages/endroid/qr-code)
8 [![Monthly Downloads](http://img.shields.io/packagist/dm/endroid/qr-code.svg)](https://packagist.org/packages/endroid/qr-code)
9 [![License](http://img.shields.io/packagist/l/endroid/qr-code.svg)](https://packagist.org/packages/endroid/qr-code)
10
11 This library helps you generate QR codes in a jiffy. Makes use of [bacon/bacon-qr-code](https://github.com/Bacon/BaconQrCode)
12 to generate the matrix and [khanamiryan/qrcode-detector-decoder](https://github.com/khanamiryan/php-qrcode-detector-decoder)
13 for validating generated QR codes. Further extended with Twig extensions, generation routes, a factory and a
14 Symfony bundle for easy installation and configuration. Different writers are provided to generate the QR code
15 as PNG, SVG, EPS or in binary format.
16
17 ## Sponsored by
18
19 [![Blackfire.io](.github/blackfire.png)](https://www.blackfire.io)
20
21 ## Installation
22
23 Use [Composer](https://getcomposer.org/) to install the library. Also make sure you have enabled and configured the
24 [GD extension](https://www.php.net/manual/en/book.image.php) if you want to generate images.
25
26 ``` bash
27 composer require endroid/qr-code
28 ```
29
30 ## Usage: using the builder
31
32 ```php
33 use Endroid\QrCode\Builder\Builder;
34 use Endroid\QrCode\Encoding\Encoding;
35 use Endroid\QrCode\ErrorCorrectionLevel;
36 use Endroid\QrCode\Label\LabelAlignment;
37 use Endroid\QrCode\Label\Font\NotoSans;
38 use Endroid\QrCode\RoundBlockSizeMode;
39 use Endroid\QrCode\Writer\PngWriter;
40
41 $result = Builder::create()
42 ->writer(new PngWriter())
43 ->writerOptions([])
44 ->data('Custom QR code contents')
45 ->encoding(new Encoding('UTF-8'))
46 ->errorCorrectionLevel(ErrorCorrectionLevel::High)
47 ->size(300)
48 ->margin(10)
49 ->roundBlockSizeMode(RoundBlockSizeMode::Margin)
50 ->logoPath(__DIR__.'/assets/symfony.png')
51 ->logoResizeToWidth(50)
52 ->logoPunchoutBackground(true)
53 ->labelText('This is the label')
54 ->labelFont(new NotoSans(20))
55 ->labelAlignment(LabelAlignment::Center)
56 ->validateResult(false)
57 ->build();
58 ```
59
60 ## Usage: without using the builder
61
62 ```php
63 use Endroid\QrCode\Color\Color;
64 use Endroid\QrCode\Encoding\Encoding;
65 use Endroid\QrCode\ErrorCorrectionLevel;
66 use Endroid\QrCode\QrCode;
67 use Endroid\QrCode\Label\Label;
68 use Endroid\QrCode\Logo\Logo;
69 use Endroid\QrCode\RoundBlockSizeMode;
70 use Endroid\QrCode\Writer\PngWriter;
71 use Endroid\QrCode\Writer\ValidationException;
72
73 $writer = new PngWriter();
74
75 // Create QR code
76 $qrCode = QrCode::create('Life is too short to be generating QR codes')
77 ->setEncoding(new Encoding('UTF-8'))
78 ->setErrorCorrectionLevel(ErrorCorrectionLevel::Low)
79 ->setSize(300)
80 ->setMargin(10)
81 ->setRoundBlockSizeMode(RoundBlockSizeMode::Margin)
82 ->setForegroundColor(new Color(0, 0, 0))
83 ->setBackgroundColor(new Color(255, 255, 255));
84
85 // Create generic logo
86 $logo = Logo::create(__DIR__.'/assets/symfony.png')
87 ->setResizeToWidth(50)
88 ->setPunchoutBackground(true)
89 ;
90
91 // Create generic label
92 $label = Label::create('Label')
93 ->setTextColor(new Color(255, 0, 0));
94
95 $result = $writer->write($qrCode, $logo, $label);
96
97 // Validate the result
98 $writer->validateResult($result, 'Life is too short to be generating QR codes');
99 ```
100
101 ## Usage: working with results
102
103 ```php
104
105 // Directly output the QR code
106 header('Content-Type: '.$result->getMimeType());
107 echo $result->getString();
108
109 // Save it to a file
110 $result->saveToFile(__DIR__.'/qrcode.png');
111
112 // Generate a data URI to include image data inline (i.e. inside an <img> tag)
113 $dataUri = $result->getDataUri();
114 ```
115
116 ![QR Code](.github/example.png)
117
118 ### Writer options
119
120 Some writers provide writer options. Each available writer option is can be
121 found as a constant prefixed with WRITER_OPTION_ in the writer class.
122
123 * `PdfWriter`
124 * `unit`: unit of measurement (default: mm)
125 * `fpdf`: PDF to place the image in (default: new PDF)
126 * `x`: image offset (default: 0)
127 * `y`: image offset (default: 0)
128 * `link`: a URL or an identifier returned by `AddLink()`.
129 * `PngWriter`
130 * `compression_level`: compression level (0-9, default: -1 = zlib default)
131 * `SvgWriter`
132 * `block_id`: id of the block element for external reference (default: block)
133 * `exclude_xml_declaration`: exclude XML declaration (default: false)
134 * `exclude_svg_width_and_height`: exclude width and height (default: false)
135 * `force_xlink_href`: forces xlink namespace in case of compatibility issues (default: false)
136 * `compact`: create using `path` element, otherwise use `defs` and `use` (default: true)
137 * `WebPWriter`
138 * `quality`: image quality (0-100, default: 80)
139
140 You can provide any writer options like this.
141
142 ```php
143 use Endroid\QrCode\Writer\SvgWriter;
144
145 $builder->writerOptions([
146 SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true
147 ]);
148 ```
149
150 ### Encoding
151
152 If you use a barcode scanner you can have some troubles while reading the
153 generated QR codes. Depending on the encoding you chose you will have an extra
154 amount of data corresponding to the ECI block. Some barcode scanner are not
155 programmed to interpret this block of information. To ensure a maximum
156 compatibility you can use the `ISO-8859-1` encoding that is the default
157 encoding used by barcode scanners (if your character set supports it,
158 i.e. no Chinese characters are present).
159
160 ### Round block size mode
161
162 By default block sizes are rounded to guarantee sharp images and improve
163 readability. However some other rounding variants are available.
164
165 * `margin (default)`: the size of the QR code is shrunk if necessary but the size
166 of the final image remains unchanged due to additional margin being added.
167 * `enlarge`: the size of the QR code and the final image are enlarged when
168 rounding differences occur.
169 * `shrink`: the size of the QR code and the final image are
170 shrunk when rounding differences occur.
171 * `none`: No rounding. This mode can be used when blocks don't need to be rounded
172 to pixels (for instance SVG).
173
174 ## Readability
175
176 The readability of a QR code is primarily determined by the size, the input
177 length, the error correction level and any possible logo over the image so you
178 can tweak these parameters if you are looking for optimal results. You can also
179 check $qrCode->getRoundBlockSize() value to see if block dimensions are rounded
180 so that the image is more sharp and readable. Please note that rounding block
181 size can result in additional padding to compensate for the rounding difference.
182 And finally the encoding (default UTF-8 to support large character sets) can be
183 set to `ISO-8859-1` if possible to improve readability.
184
185 ## Validating the generated QR code
186
187 If you need to be extra sure the QR code you generated is readable and contains
188 the exact data you requested you can enable the validation reader, which is
189 disabled by default. You can do this either via the builder or directly on any
190 writer that supports validation. See the examples above.
191
192 Please note that validation affects performance so only use it in case of problems.
193
194 ## Symfony integration
195
196 The [endroid/qr-code-bundle](https://github.com/endroid/qr-code-bundle)
197 integrates the QR code library in Symfony for an even better experience.
198
199 * Configure your defaults (like image size, default writer etc.)
200 * Support for multiple configurations and injection via aliases
201 * Generate QR codes for defined configurations via URL like /qr-code/<config>/Hello
202 * Generate QR codes or URLs directly from Twig using dedicated functions
203
204 Read the [bundle documentation](https://github.com/endroid/qr-code-bundle)
205 for more information.
206
207 ## Versioning
208
209 Version numbers follow the MAJOR.MINOR.PATCH scheme. Backwards compatibility
210 breaking changes will be kept to a minimum but be aware that these can occur.
211 Lock your dependencies for production and test your code when upgrading.
212
213 ## License
214
215 This bundle is under the MIT license. For the full copyright and license
216 information please view the LICENSE file that was distributed with this source code.