chillerlan/php-qrcode

A QR code generator and reader with a user friendly API. PHP 7.4+

4.3.4 2022-07-25 09:12 UTC

README

A PHP QR Code library based on the implementation by Kazuhiko Arase, namespaced, cleaned up, improved and other stuff. It also features a QR Code reader which is based on the ZXing library.

Attention: there is now also a javascript port: chillerlan/js-qrcode.

PHP Version Support Packagist version License Continuous Integration CodeCov Scrunitizer CI Packagist downloads

Features

  • Generation of Model 2 QR Codes, Version 1 to 40
  • ECC Levels L/M/Q/H supported
  • Mixed mode support (encoding modes can be mixed as needed within a QR symbol)
    • Supported modes: numeric, alphanumeric, 8-bit binary as well as the 13-bit double-byte modes kanji (Japanese, Shift-JIS) and hanzi (simplified Chinese, GB2312/GB18030)
  • Flexible, easily extensible output modules
    • Built-in support for the following output formats:
      • GdImage
      • ImageMagick
      • Markup types: SVG, HTML, etc.
      • String types: JSON, plain text, etc.
      • Encapsulated Postscript (EPS)
      • PDF via FPDF
  • QR Code reader (via GD and ImageMagick)

Documentation

See the wiki for advanced documentation. An API documentation created with phpDocumentor can be found at https://chillerlan.github.io/php-qrcode/ (WIP).

Requirements

For the QRCode reader, either ext-gd or ext-imagick is required!

Installation

requires composer

via terminal:

composer require chillerlan/php-qrcode

via composer.json:

{
	"require": {
		"php": "^7.4 || ^8.0",
		"chillerlan/php-qrcode": "dev-main"
	}
}

Note: replace dev-main with a version constraint, e.g. ^4.3 - see releases for valid versions. See the installation guide on the wiki for more info!

Quickstart

We want to encode this URI for a mobile authenticator into a QRcode image:

$data = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net';

// quick and simple:
echo '<img src="'.(new QRCode)->render($data).'" alt="QR Code" />';

QR codes are awesome!

Wait, what was that? Please again, slower! See Advanced usage on the wiki. Also, have a look in the examples folder for some more usage examples.

Reading QR Codes

Using the built-in QR Code reader is pretty straight-forward:

$result = (new QRCode)->readFromFile('path/to/file.png'); // -> DecoderResult

// you can now use the result instance...
$content = $result->data;
$matrix  = $result->getMatrix(); // -> QRMatrix

// ...or simply cast it to string to get the content:
$content = (string)$result;

It's generally a good idea to wrap the reading in a try/catch block to handle any errors that may occur in the process.

Framework Integration

Shameless advertising

Hi, please check out my other projects that are way cooler than qrcodes!

Disclaimer!

I don't take responsibility for molten CPUs, misled applications, failed log-ins etc.. Use at your own risk!

License notice

Parts of this code are ported to php from the ZXing project and licensed under the Apache License, Version 2.0.

Trademark Notice

The word "QR Code" is a registered trademark of DENSO WAVE INCORPORATED
https://www.qrcode.com/en/faq.html#patentH2Title