289
qrdecode
qrdecode
SYNOPSIS
qrdecode($qr, $type='file', $imagick=false)
DESCRIPTION
qrdecode
returns the character string read in an image which contains a QR code.
If $type
is 'file'
, $qr
is the path name of a file of an image.
If $type
is 'blob'
, $qr
is an image in its binary format.
If $type
is 'resource'
, $qr
is a resource
of an image.
If imagick
is true
and if the extension imagick
is installed, ImageMagick is used to read the content of $qr
.
qrdecode
returns false
if a QR code could not be read in $qr
.
CODE
- require_once 'vendor/autoload.php';
- use zxing\QrReader;
- function qrdecode($qr, $type='file', $imagick=false) {
- if (!$qr or !$type or !in_array($type, array('file', 'blob', 'resource'))) {
- return false;
- }
- $qrcode = new Zxing\QrReader($qr, $type, $imagick);
- return $qrcode !== false ? $qrcode->text() : false;
- }
IMPORTANT: Install QR code decoder / reader for PHP, a port from the ZXing library, at the root of the website:
$ composer require zxing/qr-reader
EXAMPLE
$ php -a
php > require_once 'library/qrdecode.php';
php > echo qrdecode('qr.png', 'file');
php > echo qrdecode(file_get_contents('qr.png'), 'blob');
php > echo qrdecode(imagecreatefrompng('qr.png'), 'resource');
Comments