Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork768
PHP QR Code library that helps you create QR codes in a jiffy
License
endroid/qr-code
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Byendroid
All my work is coffeerighted ☕ 😉
If you like my work please support me by visiting thesponsor page or you canbuy me a coffee ☕
This library helps you generate QR codes in a jiffy. Makes use ofbacon/bacon-qr-codeto generate the matrix andkhanamiryan/qrcode-detector-decoderfor validating generated QR codes. Further extended with Twig extensions, generation routes, a factory and aSymfony bundle for easy installation and configuration. Different writers are provided to generate the QR codeas PNG, WebP, SVG, EPS or in binary format.
UseComposer to install the library. Also make sure you have enabled and configured theGD extension if you want to generate images.
composer require endroid/qr-codeuse Endroid\QrCode\Builder\Builder;use Endroid\QrCode\Encoding\Encoding;use Endroid\QrCode\ErrorCorrectionLevel;use Endroid\QrCode\Label\LabelAlignment;use Endroid\QrCode\Label\Font\OpenSans;use Endroid\QrCode\RoundBlockSizeMode;use Endroid\QrCode\Writer\PngWriter;$builder = new Builder( writer: new PngWriter(), writerOptions: [], validateResult: false, data: 'Custom QR code contents', encoding: new Encoding('UTF-8'), errorCorrectionLevel: ErrorCorrectionLevel::High, size: 300, margin: 10, roundBlockSizeMode: RoundBlockSizeMode::Margin, logoPath: __DIR__.'/assets/bender.png', logoResizeToWidth: 50, logoPunchoutBackground: true, labelText: 'This is the label', labelFont: new OpenSans(20), labelAlignment: LabelAlignment::Center);$result = $builder->build();use Endroid\QrCode\Color\Color;use Endroid\QrCode\Encoding\Encoding;use Endroid\QrCode\ErrorCorrectionLevel;use Endroid\QrCode\QrCode;use Endroid\QrCode\Label\Label;use Endroid\QrCode\Logo\Logo;use Endroid\QrCode\RoundBlockSizeMode;use Endroid\QrCode\Writer\PngWriter;use Endroid\QrCode\Writer\ValidationException;$writer = new PngWriter();// Create QR code$qrCode = new QrCode( data: 'Life is too short to be generating QR codes', encoding: new Encoding('UTF-8'), errorCorrectionLevel: ErrorCorrectionLevel::Low, size: 300, margin: 10, roundBlockSizeMode: RoundBlockSizeMode::Margin, foregroundColor: new Color(0, 0, 0), backgroundColor: new Color(255, 255, 255));// Create generic logo$logo = new Logo( path: __DIR__.'/assets/bender.png', resizeToWidth: 50, punchoutBackground: true);// Create generic label$label = new Label( text: 'Label', textColor: new Color(255, 0, 0));$result = $writer->write($qrCode, $logo, $label);// Validate the result$writer->validateResult($result, 'Life is too short to be generating QR codes');// Directly output the QR codeheader('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();Some writers provide writer options. Each available writer option is can befound as a constant prefixed with WRITER_OPTION_ in the writer class.
PdfWriterunit: unit of measurement (default: mm)fpdf: PDF to place the image in (default: new PDF)x: image offset (default: 0)y: image offset (default: 0)link: a URL or an identifier returned byAddLink().
PngWritercompression_level: compression level (0-9, default: -1 = zlib default)number_of_colors: number of colors (1-256, null for true color and transparency)
SvgWriterblock_id: id of the block element for external reference (default: block)exclude_xml_declaration: exclude XML declaration (default: false)exclude_svg_width_and_height: exclude width and height (default: false)force_xlink_href: forces xlink namespace in case of compatibility issues (default: false)compact: create usingpathelement, otherwise usedefsanduse(default: true)
WebPWriterquality: image quality (0-100, default: 80)
You can provide any writer options like this.
use Endroid\QrCode\Writer\SvgWriter;$builder = new Builder( writer: new SvgWriter(), writerOptions: [ SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true ]);If you use a barcode scanner you can have some troubles while reading thegenerated QR codes. Depending on the encoding you chose you will have an extraamount of data corresponding to the ECI block. Some barcode scanner are notprogrammed to interpret this block of information. To ensure a maximumcompatibility you can use theISO-8859-1 encoding that is the defaultencoding used by barcode scanners (if your character set supports it,i.e. no Chinese characters are present).
By default block sizes are rounded to guarantee sharp images and improvereadability. However some other rounding variants are available.
margin (default): the size of the QR code is shrunk if necessary but the sizeof the final image remains unchanged due to additional margin being added.enlarge: the size of the QR code and the final image are enlarged whenrounding differences occur.shrink: the size of the QR code and the final image areshrunk when rounding differences occur.none: No rounding. This mode can be used when blocks don't need to be roundedto pixels (for instance SVG).
The readability of a QR code is primarily determined by the size, the inputlength, the error correction level and any possible logo over the image so youcan tweak these parameters if you are looking for optimal results. You can alsocheck $qrCode->getRoundBlockSize() value to see if block dimensions are roundedso that the image is more sharp and readable. Please note that rounding blocksize can result in additional padding to compensate for the rounding difference.And finally the encoding (default UTF-8 to support large character sets) can beset toISO-8859-1 if possible to improve readability.
If you need to be extra sure the QR code you generated is readable and containsthe exact data you requested you can enable the validation reader, which isdisabled by default. You can do this either via the builder or directly on anywriter that supports validation. See the examples above.
Please note that validation affects performance so only use it in case of problems.
Theendroid/qr-code-bundleintegrates the QR code library in Symfony for an even better experience.
- Configure your defaults (like image size, default writer etc.)
- Support for multiple configurations and injection via aliases
- Generate QR codes for defined configurations via URL like /qr-code//Hello
- Generate QR codes or URLs directly from Twig using dedicated functions
Read thebundle documentationfor more information.
Version numbers follow the MAJOR.MINOR.PATCH scheme. Backwards compatibilitybreaking 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.
This bundle is under the MIT license. For the full copyright and licenseinformation please view the LICENSE file that was distributed with this source code.
About
PHP QR Code library that helps you create QR codes in a jiffy
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.

