The simple types in QR_Code\Types encode on every call. For repeated or batch generation the library has two cached generators, FastQRCode and UltraFastQRCode, and a benchmark class you can run on your own hardware.
Note: The README and CHANGELOG state a "3300x" speedup for cache hits. The repository contains no recorded measurement for it, no hardware and no PHP version, so this page gives no numbers. Run the benchmark below to measure your own environment.
FastQRCode
FastQRCode caches both the QR matrix and the rendered output. The default cache is a MemoryCache; pass a FileCache to persist results between requests. A cached result is only used when you do not pass a filename.
use QR_Code\Cache\FileCache;use QR_Code\Cache\MemoryCache;use QR_Code\Config\QRConfig;use QR_Code\Enums\ErrorCorrectionLevel;use QR_Code\FastQRCode; $fast = new FastQRCode(new MemoryCache); // or new FastQRCode(new FileCache) $config = QRConfig::default()->with( errorCorrectionLevel: ErrorCorrectionLevel::Medium, size: 6,); $png = $fast->png('Hello World', $config); // data URI$svg = $fast->svg('Hello World', $config); // SVG string $stats = $fast->getCacheStats();$fast->clearCache();
Constructor: new FastQRCode(?CacheInterface $cache = null, ?ImageEngine $preferredEngine = null, bool $enableCache = true). FileCache defaults to sys_get_temp_dir().'/qrcode_cache' and a one hour TTL.
Warning: In v1.0.2
FastQRCode::svg()andUltraFastQRCode::generate()with thesvgformat only render modules with the Imagick engine. Both use the same renderer, and with the GD engine they return an empty SVG (background only) and emit warnings.
Presets on QRConfig: default(), highQuality() and compact(). QRConfig accepts a size from 1 to 50 and a margin from 0 to 20.
Batch generation
use QR_Code\FastQRCode; $fast = new FastQRCode; $items = [ 'home' => 'https://example.com', 'docs' => 'https://example.com/docs',]; $codes = $fast->batch($items); // same keys, data URI values
UltraFastQRCode
UltraFastQRCode adds compressed matrix caching, cache warming and performance statistics. It takes a ModernQRConfig.
use QR_Code\Config\ModernQRConfig;use QR_Code\Enums\ErrorCorrectionLevel;use QR_Code\UltraFastQRCode; $qr = new UltraFastQRCode; $config = ModernQRConfig::default() ->setSize(5) ->setErrorCorrectionLevel(ErrorCorrectionLevel::High); $warmed = $qr->warmCache(['https://example.com', 'https://example.org'], $config); $png = $qr->generate('https://example.com', $config); $stats = $qr->getPerformanceStats();
Note:
warmCache()caches PNG output only, so keep warmed items on the defaultpngformat. Any other format rebuilds the image from a compressed matrix whose size is estimated from the data length in v1.0.2.
ModernQRConfig::default(), highQuality() and compact() are available as presets. Constructor: new UltraFastQRCode(?CacheInterface $cache = null, ?ImageEngine $preferredEngine = null, bool $enableCache = true, bool $enableMonitoring = true). Passing enableMonitoring: false throws a TypeError in v1.0.2, so leave monitoring enabled.
Image engines
ImageEngine has the cases GD, Imagick and None. detectBest() returns Imagick when the extension is loaded, then GD. FastQRCode and UltraFastQRCode use the detected engine unless you pass one.
use QR_Code\Enums\ImageEngine;use QR_Code\FastQRCode; $available = ImageEngine::getAvailable();$best = ImageEngine::detectBest(); $fast = new FastQRCode;$fast->setImageEngine(ImageEngine::GD); // throws RuntimeException if unavailable
PNG output from the types and QR_Code::png() always goes through GD; svg() and eps() write vector text without an image extension.
Frame cache
Tools::buildCache() fills the in-memory frame cache and writes frame_N.png images to QR_CACHE_DIR; masks are only kept when QR_CACHEABLE is defined as true before autoload. Tools::clearCache() resets only Tools' own static frame list, which the encoder does not use, so it does not clear the frame cache. Neither one caches generated QR codes.
use QR_Code\Util\Tools; Tools::buildCache();Tools::clearCache();
Matrix helpers
ArrayOptimizer compresses a QR matrix with zlib (gzcompress), which UltraFastQRCode uses for its matrix cache.
use QR_Code\QR_Code;use QR_Code\Util\ArrayOptimizer; $matrix = QR_Code::text('Hello');$compressed = ArrayOptimizer::compressMatrix($matrix);$restored = ArrayOptimizer::decompressMatrix($compressed, count($matrix));
Measure it yourself
QR_Code\Performance\Benchmark compares the original encoder with FastQRCode, the image engines, cache hits and misses, and batch generation. It prints its results to the output and returns them as an array.
use QR_Code\Performance\Benchmark; $results = (new Benchmark)->runBenchmark();
For a single measurement, QR_Code\Util\Benchmark records named time marks:
use QR_Code\Types\QR_Text;use QR_Code\Util\Benchmark; Benchmark::mark('start');(new QR_Text('Benchmark test'))->png();Benchmark::mark('end'); print_r(Benchmark::getResults()); // seconds between marks
The encoder records its own marks (after_raw, after_filler, after_mask, after_encode) in the same list and getResults() adds a finish mark, so the results include those entries and Total runs from the first recorded mark.
Run it on this server
The button below generates 50 PNG QR codes with QR_Text on the server that hosts these docs and reports the time it took. The numbers describe this server at this moment, not your environment.
Generates 50 PNG QR codes with the loop below and times it.
$start = microtime(true);
for ($i = 1; $i <= 50; $i++) {
$qr = new QR_Text("Performance Test #$i");
$qr->setSize(3)->setErrorCorrectionLevel('L');
$qr->png();
}
$totalMs = (microtime(true) - $start) * 1000;
Practical settings
- A larger size or margin produces a larger image; a higher error correction level adds error correction data and can need a larger QR version.
- Reuse the
FastQRCodeinstance across calls, since the cache lives in it (or in theFileCachedirectory). - Install ImageMagick if you use
FastQRCodeorUltraFastQRCodeand want the Imagick engine.