Skip to content

Commit fb294e3

Browse files
authored
Responsive image fix (alshedivat#2014)
Fixes alshedivat#1967 Update the way responsive images are used to only use one srcset, and add corresponding `sizes` attribute to the relevant layouts and templates. I did not go through and add `sizes` to all the example posts/projects. When `sizes` is not specified by the user, the `figure.html` template defaults to 95% the width of the viewport which should work fine for most cases; users can optimize further if they wish by feeding `sizes` into the template. Additionally: - Enabled support for .gif to .webp compression - fix error in jekyll-imagemagick config where all images were resized to be 800px or less. (for example img-1400.webp was actually only 800px wide, etc.) - added note about making sure imagemagick is installed before enabling it in responsive images section of `_config.yml`
1 parent 70d9ca9 commit fb294e3

File tree

7 files changed

+33
-21
lines changed

7 files changed

+33
-21
lines changed

_config.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ external_links:
329329
# Responsive WebP Images
330330
# -----------------------------------------------------------------------------
331331

332+
# MAKE SURE imagemagick is installed and on your PATH before enabling imagemagick. In a terminal, run:
333+
# convert -version
332334
imagemagick:
333335
enabled: true # enables responsive images for your site (recommended, see https://github.com/alshedivat/al-folio/issues/537)
334336
widths:
@@ -342,8 +344,9 @@ imagemagick:
342344
- ".jpeg"
343345
- ".png"
344346
- ".tiff"
347+
- ".gif"
345348
output_formats:
346-
webp: "-resize 800x"
349+
webp: "-quality 85 -strip"
347350

348351
# -----------------------------------------------------------------------------
349352
# Jekyll Diagrams

_includes/figure.html

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
{%- assign img_path = include.path | remove: ".jpg" | remove: ".jpeg" | remove: ".png" | remove: ".tiff" -%}
1+
{%- assign img_path = include.path | remove: ".jpg" | remove: ".jpeg" | remove: ".png" | remove: ".tiff" | remove: ".gif" -%}
22

33
<figure>
4-
54
<picture>
5+
<!-- Auto scaling with imagemagick -->
6+
<!-- See https://www.debugbear.com/blog/responsive-images#w-descriptors-and-the-sizes-attribute and
7+
https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images for info on defining 'sizes' for responsive images-->
68
{% if site.imagemagick.enabled %}
7-
{% for i in site.imagemagick.widths -%}
8-
<source
9-
class="responsive-img-srcset"
10-
media="(max-width: {{ i }}px)"
11-
srcset="{{ img_path | relative_url }}-{{ i }}.webp"
12-
/>
13-
{% endfor -%}
9+
<source class = "responsive-img-srcset" srcset="
10+
{% for i in site.imagemagick.widths -%}
11+
{{ img_path | relative_url }}-{{ i }}.webp {{i}}w,
12+
{% endfor -%}"
13+
{% if include.sizes %} sizes="{{include.sizes}}" {% else %} sizes="95vw" {% endif %}
14+
type="image/webp"/>
1415
{% endif %}
15-
16-
<!-- Fallback to the original file -->
1716
<img
1817
src="{% if include.cache_bust %}{{ include.path | relative_url | bust_file_cache }}{% else %}{{ include.path | relative_url }}{% endif %}"
1918
{% if include.class %}class="{{ include.class }}"{% endif %}
20-
{% if include.width %}width="{{ include.width }}"{% else %}width="auto"{% endif %}
19+
{% if include.width %}width="{{ include.width }}"{% else %}width="100%"{% endif %}
2120
{% if include.height %}height="{{ include.height }}"{% else %}height="auto"{% endif %}
2221
{% if include.min-width %}min-width="{{ include.min-width }}"{% endif %}
2322
{% if include.min-height %}min-height="{{ include.min-height }}"{% endif %}

_includes/projects.html

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{%- if project.img %}
1111
{%- include figure.html
1212
path=project.img
13+
sizes = "250px"
1314
alt="project thumbnail" -%}
1415
{%- endif %}
1516
<div class="card-body">

_includes/projects_horizontal.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<div class="row g-0">
99
{%- if project.img -%}
1010
<div class="card-img col-md-6">
11-
{% include figure.html path=project.img alt="project thumbnail" %}
11+
{% include figure.html path=project.img sizes="(min-width: 768px) 156px, 50vw" alt="project thumbnail" %}
1212
</div>
1313
<div class="col-md-6">
1414
{%- else -%}

_layouts/about.html

+8-4
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ <h1 class="post-title">
2323
{%- assign profile_image_class = "img-fluid z-depth-1 rounded" -%}
2424
{% endif %}
2525

26+
{% capture sizes %}(min-width: {{site.max_width}}) {{ site.max_width | minus: 30 | times: 0.3}}px, (min-width: 576px) 30vw, 95vw"{% endcapture %}
27+
2628
{% include figure.html
27-
path=profile_image_path
28-
class=profile_image_class
29-
alt=page.profile.image
30-
cache_bust=true -%}
29+
path = profile_image_path
30+
class = profile_image_class
31+
sizes = sizes
32+
alt = page.profile.image
33+
cache_bust = true
34+
%}
3135
{% endif -%}
3236
{%- if page.profile.more_info %}
3337
<div class="more-info">

_layouts/bib.html

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
{%- assign entry_path = entry.preview | prepend: '/assets/img/publication_preview/' -%}
1212
{% include figure.html
1313
path=entry_path
14+
sizes = "200px"
1415
class="preview z-depth-1 rounded"
1516
zoomable=false
1617
alt=entry.preview -%}

_layouts/profiles.html

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
{%- assign profile_image_class = "img-fluid z-depth-1 rounded" -%}
2020
{% endif %}
2121

22+
{% capture sizes %}(min-width: {{site.max_width}}) {{ site.max_width | minus: 30 | times: 0.3}}px, (min-width: 576px) 30vw, 95vw"{% endcapture %}
23+
2224
{% include figure.html
23-
path=profile_image_path
24-
class=profile_image_class
25-
alt=profile.image -%}
25+
path = profile_image_path
26+
class = profile_image_class
27+
sizes = sizes
28+
alt = profile.image
29+
%}
2630
{% endif -%}
2731
{%- if profile.more_info %}
2832
<div class="more-info">

0 commit comments

Comments
 (0)