Skip to content

Commit 5226d63

Browse files
committed
responsive images
1 parent e4d0051 commit 5226d63

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,22 @@ block-style automatic rendering is also supported, if enabled in the config:
9494
katex_enable = true
9595
katex_auto_render = true
9696
```
97+
98+
### Responsive Images
99+
To create images in multiple resolutions you can use replace `![some image](foo)`
100+
with `{{ image(src="foo.jpeg", alt="some image") }}`. You can configure image
101+
sizes in "width in pixels":
102+
103+
```toml
104+
[extra]
105+
images_default_size = 1000
106+
images_sizes = [500, 1000, 2000]
107+
```
108+
109+
The browser will automatically choose an appropriate image size based on the screen
110+
size and other factors like connectivity. For browser that do not support responsive
111+
images, `images_default_size` will be used.
112+
113+
Please note that specifying many entries in `images_sizes` will slow down `zola serve`
114+
and `zola build` considerably. Since one file per size is created, this also potentially
115+
increases the hosting costs of your website.

config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ even_menu = [
2222
{url = "$BASE_URL/tags", name = "Tags"},
2323
{url = "$BASE_URL/about", name = "About"},
2424
]
25+
images_default_size = 1000
26+
images_sizes = [500, 1000, 2000]
2527

2628
even_title = "Even"

templates/shortcodes/image.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% set abspath = "./" ~ page.path ~ src %}
2+
{% set meta = get_image_metadata(path=abspath) %}
3+
{% set width = meta.width %}
4+
{% set srcset_list = [] %}
5+
{% for s in config.extra.images_sizes %}
6+
{% if width >= s %}
7+
{% set resized = resize_image(path=abspath, width=s, op="fit_width") %}
8+
{% set element = resized.url ~ " " ~ s ~ "w" %}
9+
{% set_global srcset_list = srcset_list | concat(with=[element]) %}
10+
{% endif %}
11+
{% endfor %}
12+
{% set default_resized = resize_image(path=abspath, width=config.extra.images_default_size, op="fit_width") %}
13+
<img alt="{{ alt }}" src="{{ default_resized.url | safe }}" srcset="{{ srcset_list | join(sep=", ") | safe }}" />

0 commit comments

Comments
 (0)