Dynamically generating a QR code with PHP

Dynamically generating a QR code with PHP

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Dynamically generating a QR code with PHP

Generating QR codes dynamically in PHP usually means you are trying to take a variable—like a user-submitted URL or a product identifier—and render it into an image on the fly. The immediate question is whether to build this from scratch or rely on a library. Building a QR generator from raw bitwise operations and error correction algorithms is possible but impractical for production; the math behind Reed-Solomon error correction and matrix masking is non-trivial. Instead, you should leverage established libraries that handle these complexities under the hood.

The most straightforward approach in PHP is to use endroid/qr-code. This package is widely adopted because it abstracts away the rendering engine while offering flexibility in output formats like PNG, JPG, or SVG. To get started, you need to install the dependency via Composer:

composer require endroid/qr-code

Once installed, instantiation is minimal. You create an instance of Endroid\QrCode\QrCode, set your data string, and then generate the image. However, simply generating a raw binary QR code often leads to usability issues. A standard QR code might be difficult to scan if it lacks contrast or if the scanning environment has poor lighting. The library provides options to enhance readability, such as adding a logo overlay or adjusting the error correction level.

Error correction is a critical trade-off to understand when designing these codes. QR codes use Reed-Solomon algorithms to tolerate damage. The levels are L (7%), M (15%), Q (25%), and H (30%). If you plan to embed a large logo over the center of the code, you must increase the error correction level; otherwise, the data payload plus the visual obstruction will exceed the capacity of the chosen level, rendering the code unreadable. Conversely, using the highest error correction level consumes more space for redundancy, meaning less room for actual data if you aren't overlaying anything.

Another common pitfall involves image dimensions and resolution. A QR code is a grid of modules (black and white squares). If you generate an image that is too small, say 100x100 pixels, the individual modules might blend together depending on the DPI or the screen resolution of the scanner. This is particularly relevant for mobile scanning; a phone camera expects a certain pixel density to decode the pattern accurately. It is often better to let the library calculate the optimal size based on your content rather than forcing a fixed dimension that might be too low.

If you are working within the Laravel ecosystem, integrating this functionality can be seamless. You might define a helper method in your service container or use the facade to keep your controllers clean. Here is how you might encapsulate the generation logic:

use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;

function generateQrCode(string $url): string {
    $qrCode = new QrCode($url);
    $writer = new PngWriter();
    
    // Set error correction to H if you plan to add a logo later
    $qrCode->setErrorCorrectionLevel(\Endroid\QrCode\ErrorCorrectionLevel::High);
    
    $pngString = $writer->write($qrCode)->getString();
    return $pngString;
}

This approach returns the image as a base64 string or binary data, which can be easily returned in an HTTP response or saved to disk. If you are building a feature where users expect a branded experience, consider adding a logo. The library supports setting an image property on the QR code instance, allowing you to overlay a PNG file. Just remember to keep the logo small and centered to avoid breaking the module grid.

For production environments, performance matters. If your application needs to generate thousands of QR codes per minute, generating them on-the-fly in PHP adds CPU overhead. In such cases, consider caching the generated images or moving the generation logic to a background job queue if the request latency becomes a bottleneck. Tools like Laravel Queues can handle this by offloading the image generation to a worker process.

If you require more advanced features, such as generating QR codes with custom colors, quiet zones, or specific file formats like SVG for vector scalability, check the available writers and configuration options in the documentation. The flexibility of endroid/qr-code allows you to tailor the output without reinventing the underlying encoding logic. For resources on best practices regarding QR code design and compatibility, refer to guides from experts in the field, such as those found at https://laravelcompany.com.

Our network