PHP library to generate QR Codes as compact, stylable SVGs. It extends the MIT-licensed QRCode implementation by Kazuhiko Arase and adds an SVG renderer with multiple visual variants (normal, rounded, fullyrounded, dotted) and color support including alpha transparency.
- Normal, rounded, fullyrounded and dotted QR styles
- Foreground/background colors with alpha (#rrggbbaa, rgba(), etc.)
- Clean, size-optimized/compact SVG output (no rasterization)
- High error correction by default (Level H)
- Simple API and demo page
Start a local PHP server and open the test page:
php -S localhost:8000Then visit http://localhost:8000/test/test_varianten.php to see the variants for the text "Test von QRCode SVG" in different colors.
Using composer and run in your project folder:
composer require github.com/janmz/qrcodesvg:dev-mainThis will include the dependency qrcode-generator/qrcode.php which is updated via a githup workflow to avoid distributing the complete package.
In the code just use:
require __DIR__ . '/vendor/autoload.php';If you use this repository directly, the provided composer.json registers a classmap for the two PHP files.
# class loaded via autoloader!
$qr = new QRCodeSVG();
$qr->setErrorCorrectLevel(QR_ERROR_CORRECT_LEVEL_H);
$qr->addData('Hello SVG');
$qr->setTypeNumber(4); // or compute automatically
$qr->make();
$svg = $qr->getSVG(
256,
'#000000ff',
'#ffffff00',
'QR Code for Hello SVG',
'rounded'
);
echo $svg;See test_varianten.php for an auto-sizing example that computes the minimal type number for the input length.
- normal: classic square modules and rectangles
- rounded: rounded connections and corners
- fullyrounded: like rounded, but concave inner corners are rounded too
- dotted: all modules as dots (no rectangles)
Pass one of the above as the variation argument to getSVG().
Foreground ($fg) and background ($bg) accept:
#rgb,#rrggbb,#rrggbbaargb(r,g,b)rgba(r,g,b,a)
The renderer normalizes colors and keeps them short where possible. Alpha is supported; a fully transparent background is the default.
QRCodeSVG::getSVG(int $width, string $fg="#000000FF", string $bg="#ffffff00", string $alt_text="", string $variation="normal"): string
- width: target width/height of the SVG canvas
- fg: foreground color
- bg: background color
- alt_text: used as
aria-label - variation:
normal,rounded,fullyrounded, ordotted
Other methods come from the base QRCode class: addData, setTypeNumber, setErrorCorrectLevel, make, etc.
- Demo page:
test_varianten.php - Core class:
qrcodesvg.php(SVG renderer) - Base class:
qrcode.phpis only importet and should not be changed. To avoid copying the complete repository of the base class, which also includes implementations for other languages/environments a trick is used. The current file is updated via a github workflow and included into a special folder in this repository. If You want to develop in a fork, make shure also to use the github workflow or build a small script for Your own.
Run a local server with php -S localhost:8000 and open the demo page https://localhost/test/test_varianten.php.
The project includes comprehensive regression tests to ensure visual consistency across different PHP versions and environments.
cd test
php regression_test.php- Visual regression tests with PNG comparison (60 test cases)
- Multiple QR code variants (normal, rounded, fullyrounded, dotted)
- Color combinations with transparency
- Contour-based path rendering for compact SVG output
- Cross-platform SVG to PNG conversion
- Automatic Path/Elemente validation during baseline generation and current testing
Tests run automatically on:
- Every push to
mainordevelopbranches - Pull requests against
main - Release creation (as prerequisite)
See test/README.md for detailed testing documentation.
Open features list:
- merging the qrcode with a logo in the midde (can be done via HTML/CSS right now)
- gradients in the background
- gradients in the boxes
- gradients over the foreground
MIT. See LICENSE.
- Original QR code generator: Kazuhiko Arase (https://github.com/kazuhikoarase/qrcode-generator)
If this project is useful to you, please consider a small donation to support children in need via CFI-Kinderhilfe:
Use the following link: https://cfi-kinderhilfe.de/jetzt-spenden/?q=SVGVAYA
Thank you!