Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: responsive images #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,22 @@ block-style automatic rendering is also supported, if enabled in the config:
katex_enable = true
katex_auto_render = true
```

### Responsive Images
To create images in multiple resolutions you can use replace `![some image](foo)`
with `{{ image(src="foo.jpeg", alt="some image") }}`. You can configure image
sizes in "width in pixels":

```toml
[extra]
images_default_size = 1000
images_sizes = [500, 1000, 2000]
```

The browser will automatically choose an appropriate image size based on the screen
size and other factors like connectivity. For browser that do not support responsive
images, `images_default_size` will be used.

Please note that specifying many entries in `images_sizes` will slow down `zola serve`
and `zola build` considerably. Since one file per size is created, this also potentially
increases the hosting costs of your website.
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ even_menu = [
{url = "$BASE_URL/tags", name = "Tags"},
{url = "$BASE_URL/about", name = "About"},
]
images_default_size = 1000
images_sizes = [500, 1000, 2000]

even_title = "Even"
13 changes: 13 additions & 0 deletions templates/shortcodes/image.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% set abspath = "./" ~ page.path ~ src %}
{% set meta = get_image_metadata(path=abspath) %}
{% set width = meta.width %}
{% set srcset_list = [] %}
{% for s in config.extra.images_sizes %}
{% if width >= s %}
{% set resized = resize_image(path=abspath, width=s, op="fit_width") %}
{% set element = resized.url ~ " " ~ s ~ "w" %}
{% set_global srcset_list = srcset_list | concat(with=[element]) %}
{% endif %}
{% endfor %}
{% set default_resized = resize_image(path=abspath, width=config.extra.images_default_size, op="fit_width") %}
<img alt="{{ alt }}" src="{{ default_resized.url | safe }}" srcset="{{ srcset_list | join(sep=", ") | safe }}" />