attestra / qr-code
Attestra QR Code
Fund package maintenance!
endroid
Requires
- php: ^7.4||^8.0
- bacon/bacon-qr-code: ^2.0
Requires (Dev)
- ext-gd: *
- endroid/quality: dev-master
- khanamiryan/qrcode-detector-decoder: ^1.0.4
- setasign/fpdf: ^1.8.2
Suggests
- ext-gd: Enables you to write PNG images
- khanamiryan/qrcode-detector-decoder: Enables you to use the image validator
- roave/security-advisories: Makes sure package versions with known security issues are not installed
- setasign/fpdf: Enables you to use the PDF writer
- dev-master / 4.x-dev
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.3.5
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.x-dev
- 3.9.7
- 3.9.6
- 3.9.5
- 3.9.4
- 3.9.3
- 3.9.2
- 3.9.1
- 3.9.0
- 3.8.2
- 3.8.1
- 3.8.0
- 3.7.9
- 3.7.8
- 3.7.7
- 3.7.6
- 3.7.5
- 3.7.4
- 3.7.3
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.1
- 3.6.0
- 3.5.9
- 3.5.8
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.12
- 3.2.11
- 3.2.10
- 3.2.9
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.x-dev
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.4
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.3.4
- 1.2.4
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
This package is auto-updated.
Last update: 2024-10-25 01:07:00 UTC
README
By endroid
This library helps you generate QR codes in a jiffy. Makes use of bacon/bacon-qr-code to generate the matrix and khanamiryan/qrcode-detector-decoder for validating generated QR codes. Further extended with Twig extensions, generation routes, a factory and a Symfony bundle for easy installation and configuration. Different writers are provided to generate the QR code as PNG, SVG, EPS or in binary format.
Installation
Use Composer to install the library.
$ composer require attestra/qr-code
Usage: using the builder
use Attestra\QrCode\Builder\Builder; use Attestra\QrCode\Encoding\Encoding; use Attestra\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh; use Attestra\QrCode\Label\Alignment\LabelAlignmentCenter; use Attestra\QrCode\Label\Font\NotoSans; use Attestra\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Attestra\QrCode\Writer\PngWriter; $result = Builder::create() ->writer(new PngWriter()) ->writerOptions([]) ->data('Custom QR code contents') ->encoding(new Encoding('UTF-8')) ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) ->size(300) ->margin(10) ->roundBlockSizeMode(new RoundBlockSizeModeMargin()) ->logoPath(__DIR__.'/assets/symfony.png') ->labelText('This is the label') ->labelFont(new NotoSans(20)) ->labelAlignment(new LabelAlignmentCenter()) ->build();
Usage: without using the builder
use Attestra\QrCode\Color\Color; use Attestra\QrCode\Encoding\Encoding; use Attestra\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow; use Attestra\QrCode\QrCode; use Attestra\QrCode\Label\Label; use Attestra\QrCode\Logo\Logo; use Attestra\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Attestra\QrCode\Writer\PngWriter; $writer = new PngWriter(); // Create QR code $qrCode = QrCode::create('Data') ->setEncoding(new Encoding('UTF-8')) ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow()) ->setSize(300) ->setMargin(10) ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin()) ->setForegroundColor(new Color(0, 0, 0)) ->setBackgroundColor(new Color(255, 255, 255)); // Create generic logo $logo = Logo::create(__DIR__.'/assets/symfony.png') ->setResizeToWidth(50); // Create generic label $label = Label::create('Label') ->setTextColor(new Color(255, 0, 0)); $result = $writer->write($qrCode, $logo, $label);
Usage: working with results
// Directly output the QR code header('Content-Type: '.$result->getMimeType()); echo $result->getString(); // Save it to a file $result->saveToFile(__DIR__.'/qrcode.png'); // Generate a data URI to include image data inline (i.e. inside an <img> tag) $dataUri = $result->getDataUri();
Writer options
use Attestra\QrCode\Writer\SvgWriter; $builder->setWriterOptions([SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true]);
Encoding
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. To ensure a maximum
compatibility you can use the ISO-8859-1
encoding that is the default
encoding used by barcode scanners (if your character set supports it,
i.e. no Chinese characters are present).
Round block size mode
By default block sizes are rounded to guarantee sharp images and improve readability. However some other rounding variants are available.
margin (default)
: the size of the QR code is shrunk if necessary but the size of the final image remains unchanged due to additional margin being added.enlarge
: the size of the QR code and the final image are enlarged when rounding differences occur.shrink
: the size of the QR code and the final image are shrunk when rounding differences occur.none
: No rounding. This mode can be used when blocks don't need to be rounded to pixels (for instance SVG).
Readability
The readability of a QR code is primarily determined by the size, the input
length, the error correction level and any possible logo over the image so you
can tweak these parameters if you are looking for optimal results. You can also
check $qrCode->getRoundBlockSize() value to see if block dimensions are rounded
so that the image is more sharp and readable. Please note that rounding block
size can result in additional padding to compensate for the rounding difference.
And finally the encoding (default UTF-8 to support large character sets) can be
set to ISO-8859-1
if possible to improve readability.
Built-in validation reader
You can enable the built-in validation reader (disabled by default) by calling setValidateResult(true). This validation reader does not guarantee that the QR code will be readable by all readers but it helps you provide a minimum level of quality. Take note that the validator can consume quite amount of additional resources and it should be installed separately only if you use it.
Versioning
Version numbers follow the MAJOR.MINOR.PATCH scheme. Backwards compatibility breaking changes will be kept to a minimum but be aware that these can occur. Lock your dependencies for production and test your code when upgrading.
License
This bundle is under the MIT license. For the full copyright and license information please view the LICENSE file that was distributed with this source code.