james2001 / zxing
PHP adapter for ZXing, the Java barcode and QR code reader
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^10.5.62 || ^11.5.50 || ^12.5.8
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Read QR codes and barcodes from PHP by calling ZXing,
the Java barcode reader. The ZXing JAR files are bundled with the package: you only
need a java executable in the PATH.
Versions
| Version | Branch | PHP | Bundled ZXing | Java |
|---|---|---|---|---|
| 2.x | 2.x |
8.1 or later | 3.5.4 (core, javase, jcommander) | 8 or later |
| 1.x | 1.x |
5.4 to 8.x | 2.3 (core, javase) | 6 or later |
Composer picks the right version for your PHP version. 1.x only receives bug and security fixes.
For Symfony, see james2001/zxing-bundle.
Security
Versions before 1.1.1 let a crafted image path run shell commands
(GHSA-9wpj-79hw-46mx). Update with composer update james2001/zxing.
2.x is not affected: it starts Java without a shell.
Installation
composer require james2001/zxing
Usage
use James2001\Zxing; $zxing = new Zxing(); // First code found in the image, or false $value = $zxing->findFirst('/path/to/image.jpg'); // All the codes found in the image, or false $values = $zxing->findMulti('/path/to/image.jpg');
findFirst() and findMulti() return false when no code is found, and also
when Java cannot be run: check that java is in the PATH of your PHP process.
Prefix (key)
When a key is given, only the codes that contain it are returned, and the key is removed from the returned value:
$zxing = new Zxing('invoice;'); // QR code "invoice;F2014-001" => "F2014-001" $zxing->findFirst('/path/to/scan.jpg');
ZXing options
The second constructor argument (or setOptions()) holds extra options for the
ZXing command line runner, as a string split on whitespace or as a list of
arguments. --multi is always passed.
$zxing = new Zxing('', '--try_harder --possible_formats QR_CODE'); // same as $zxing = new Zxing('', ['--try_harder', '--possible_formats', 'QR_CODE']);
Available options: --try_harder, --pure_barcode, --products_only,
--possible_formats FORMAT[ FORMAT...], --crop LEFT TOP WIDTH HEIGHT, --brief,
--raw, --recursive, --dump_results, --dump_black_point.
Java is started without a shell, so neither the image path nor the options can inject a command.
Custom ZXing build
The third constructor argument (or setLibBinPath()) is the directory that
contains javase.jar, core.jar and jcommander.jar (ZXing 3.x). It defaults to
the bundled bin_lib/.
Upgrade from 1.x
-
PHP 8.1 or later and Java 8 or later are required.
-
The code uses strict types: the constructor and the setters only accept strings (and arrays for the options). Passing
null, for instance, throws aTypeError. -
The bundled ZXing is now 3.5.4. Its command line runner changed some options:
--possibleFormats=QR_CODEbecomes--possible_formats QR_CODE;--crop=0,0,100,100becomes--crop 0 0 100 100.
An unknown option makes ZXing fail, and
find()returnsfalse. -
Java is no longer started through a shell. The options string is split on whitespace: shell quoting is not interpreted any more. Pass an array if an argument contains spaces.
-
A custom lib bin path must contain ZXing 3.x jars, including
jcommander.jar.
Tests
The test suite needs Java:
composer install vendor/bin/phpunit
License
MIT, see LICENSE. The bundled ZXing and JCommander JAR files are distributed under the Apache License 2.0, see bin_lib/NOTICE.md.