| 1 |
--- |
| 2 |
title: images.QR |
| 3 |
description: Returns an image resource containing a QR code encoded from the given text using the specified options. |
| 4 |
keywords: [] |
| 5 |
params: |
| 6 |
functions_and_methods: |
| 7 |
aliases: [] |
| 8 |
returnType: images.ImageResource |
| 9 |
signatures: ['images.QR TEXT [OPTIONS]'] |
| 10 |
--- |
| 11 |
|
| 12 |
{{< new-in 0.141.0 />}} |
| 13 |
|
| 14 |
The `images.QR` function encodes the given text into a [QR code][] using the specified options, returning an image resource. The size of the generated image depends on three factors: |
| 15 |
|
| 16 |
- Data length: Longer text necessitates a larger image to accommodate the increased information density. |
| 17 |
- Error correction level: Higher error correction levels enhance the QR code's resistance to damage, but this typically results in a slightly larger image size to maintain readability. |
| 18 |
- Pixels per module: The number of image pixels assigned to each individual module (the smallest unit of the QR code) directly impacts the overall image size. A higher pixel count per module leads to a larger, higher-resolution image. |
| 19 |
|
| 20 |
Although the default option values are sufficient for most applications, you should test the rendered QR code both on-screen and in print. |
| 21 |
|
| 22 |
## Options |
| 23 |
|
| 24 |
The `images.QR` function accepts an options map. |
| 25 |
|
| 26 |
`level` |
| 27 |
: (`string`) The error correction level to use when encoding the text, one of `low`, `medium`, `quartile`, or `high`. Default is `medium`. |
| 28 |
|
| 29 |
Error correction level|Redundancy |
| 30 |
:--|:--|:-- |
| 31 |
low|20% |
| 32 |
medium|38% |
| 33 |
quartile|55% |
| 34 |
high|65% |
| 35 |
|
| 36 |
`scale` |
| 37 |
: (`int`) The number of image pixels per QR code module. Must be greater than or equal to `2`. Default is `4`. |
| 38 |
|
| 39 |
`targetDir` |
| 40 |
: (`string`) The subdirectory within the [`publishDir`][] where Hugo will place the generated image. Use Unix-style slashes (`/`) to separarate path segments. If empty or not provided, the image is placed directly in the `publishDir` root. Hugo automatically creates the necessary subdirectories if they don't exist. |
| 41 |
|
| 42 |
## Examples |
| 43 |
|
| 44 |
To create a QR code using the default values for `level` and `scale`: |
| 45 |
|
| 46 |
```go-html-template |
| 47 |
{{ $text := "https://gohugo.io" }} |
| 48 |
{{ with images.QR $text }} |
| 49 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 50 |
{{ end }} |
| 51 |
``` |
| 52 |
|
| 53 |
{{< qr text="https://gohugo.io" class="qrcode" targetDir="images/qr" />}} |
| 54 |
|
| 55 |
Specify `level`, `scale`, and `targetDir` as needed to achieve the desired result: |
| 56 |
|
| 57 |
```go-html-template |
| 58 |
{{ $text := "https://gohugo.io" }} |
| 59 |
{{ $opts := dict |
| 60 |
"level" "high" |
| 61 |
"scale" 3 |
| 62 |
"targetDir" "images/qr" |
| 63 |
}} |
| 64 |
{{ with images.QR $text $opts }} |
| 65 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 66 |
{{ end }} |
| 67 |
``` |
| 68 |
|
| 69 |
{{< qr text="https://gohugo.io" level="high" scale=3 targetDir="codes" class="qrcode" targetDir="images/qr" />}} |
| 70 |
|
| 71 |
To include a QR code that points to the `Permalink` of the current page: |
| 72 |
|
| 73 |
```go-html-template {file="layouts/page.html"} |
| 74 |
{{ with images.QR .Permalink }} |
| 75 |
<img |
| 76 |
src="{{ .RelPermalink }}" |
| 77 |
width="{{ .Width }}" |
| 78 |
height="{{ .Height }}" |
| 79 |
alt="QR code linking to {{ $.Permalink }}" |
| 80 |
class="qr-code" |
| 81 |
loading="lazy" |
| 82 |
> |
| 83 |
{{ end }} |
| 84 |
``` |
| 85 |
|
| 86 |
Then hide the QR code with CSS unless printing the page: |
| 87 |
|
| 88 |
```css |
| 89 |
/* Hide QR code by default */ |
| 90 |
.qr-code { |
| 91 |
display: none; |
| 92 |
} |
| 93 |
|
| 94 |
/* Show QR code when printing */ |
| 95 |
@media print { |
| 96 |
.qr-code { |
| 97 |
display: block; |
| 98 |
} |
| 99 |
} |
| 100 |
``` |
| 101 |
|
| 102 |
## Scale |
| 103 |
|
| 104 |
As you decrease the size of a QR code, the maximum distance at which it can be reliably scanned by a device also decreases. |
| 105 |
|
| 106 |
In the example above, we set the `scale` to `2`, resulting in a QR code where each module consists of 2x2 pixels. While this might be sufficient for on-screen display, it's likely to be problematic when printed at 600 dpi. |
| 107 |
|
| 108 |
\[ \frac{2\:px}{module} \times \frac{1\:inch}{600\:px} \times \frac{25.4\:mm}{1\:inch} = \frac{0.085\:mm}{module} \] |
| 109 |
|
| 110 |
This module size is half of the commonly recommended minimum of 0.170 mm.\ |
| 111 |
If the QR code will be printed, use the default `scale` value of `4` pixels per module. |
| 112 |
|
| 113 |
Avoid using Hugo's image processing methods to resize QR codes. Resizing can introduce blurring due to anti-aliasing when a QR code module occupies a fractional number of pixels. |
| 114 |
|
| 115 |
> [!NOTE] |
| 116 |
> Always test the rendered QR code both on-screen and in print. |
| 117 |
|
| 118 |
## Shortcode |
| 119 |
|
| 120 |
Call the `qr` shortcode to insert a QR code into your content. |
| 121 |
|
| 122 |
Use the self-closing syntax to pass the text as an argument: |
| 123 |
|
| 124 |
```md |
| 125 |
{{</* qr text="https://gohugo.io" /*/>}} |
| 126 |
``` |
| 127 |
|
| 128 |
Or insert the text between the opening and closing tags: |
| 129 |
|
| 130 |
```md |
| 131 |
{{</* qr */>}} |
| 132 |
https://gohugo.io |
| 133 |
{{</* /qr */>}} |
| 134 |
``` |
| 135 |
|
| 136 |
The `qr` shortcode accepts several arguments including `level` and `scale`. See the [related documentation][] for details. |
| 137 |
|
| 138 |
[QR code]: https://en.wikipedia.org/wiki/QR_code |
| 139 |
[`publishDir`]: /configuration/all/#publishdir |
| 140 |
[related documentation]: /shortcodes/qr/ |