Install the Rust QR code package, render a PNG, and register its Laravel route and template helpers.
[dependencies]
laravel-rust-qrcode = "0.1.0"
The minijinja feature is on by default and adds template functions. Set default-features = false for
a route or application that does not use views. For use without Laravel, depend on the
laravel-rust-qrcode-core crate, which exposes the encoder, renderer, and payload helpers.
Generate a code
use laravel_rust_qrcode::{Format, Generator};
let code = Generator::new()
.format(Format::Png)
.size(174)
.margin(4)
.generate("https://example.test/")?;
code.write_to("example.png")?;
Use as_bytes() to borrow the output, into_bytes() to take ownership, or as_svg() for SVG markup.
A generator can render several payloads with the same settings. Its defaults match PHP's SVG,
100-pixel canvas, zero margin, black foreground, white background, error correction L, and ISO-8859-1.
Allow a four-module quiet zone for scanning.
Use it in Laravel
Register the service provider to inject a factory or use the template functions.
use laravel::Application;
use laravel_rust_qrcode::QrCodeServiceProvider;
let app = Application::configure()
.with_providers(|providers| {
providers.add(QrCodeServiceProvider);
})
.create();
A route can inject the factory. Each call to builder() starts with fresh options.
use laravel::{container::Inject, http::Response};
use laravel_rust_qrcode::{Format, QrCodeFactory};
async fn ticket_code(qr: Inject<QrCodeFactory>) -> laravel::Result<Response> {
Ok(qr.builder()
.format(Format::Png)
.size(174)
.margin(4)
.response("https://example.test/tickets/42")?)
}
The response sets the content type for its format. In a MiniJinja view, use either helper with an optional size and margin. Both encode text as UTF-8.
<div>{{ qrcode(ticket_url, 174, 4) }}</div>
<img src="{{ qrcode_png(ticket_url, 174, 4) }}" alt="Ticket QR code">
The SVG helper marks generated markup safe. Payload text is encoded into modules. The PNG helper
returns a data URI. For email attachments, render PNG in Rust and pass into_bytes() and image/png
to your mailer.
Encoding and sizing
Use .encoding(Encoding::Utf8) when the text cannot be represented in ISO-8859-1.
generate_bytes(&[u8]) encodes opaque bytes. For a fixed QR version or mask, give the generator an
explicit encoder, for example Generator::new().with_encoder(Encoder::new().version(5).mask(2)).
Supported text encodings are UTF-8, UTF-16BE, ASCII, ISO-8859-1 through ISO-8859-11, ISO-8859-13
through ISO-8859-16, Shift-JIS, Windows-1250, Windows-1251, Windows-1252, Windows-1256, GBK, and EUC-KR.
The parser accepts SHIFT-JIS, SHIFT_JIS, ASCII, and US-ASCII case insensitively.
| Setting | Range |
|---|---|
| Canvas edge | 1 through 4096 pixels or PostScript points |
| Quiet zone | 0 through 64 modules on each side |
| QR version | 1 through 40 |
| Mask | 0 through 7 |
| RGBA alpha | 0 through 255 |
PNG needs enough pixels for at least one per module including the quiet zone. Its minimum edge is
17 + 4 × version + 2 × margin. Choose a canvas that is a multiple of that number for even module
widths. EPS composites partial transparency onto white. SVG and PNG support alpha.
See Migrating from PHP for all six payload helpers and their differences.