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

Responsive image fix #2014

Merged
merged 3 commits into from
Dec 26, 2023
Merged
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
5 changes: 4 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ external_links:
# Responsive WebP Images
# -----------------------------------------------------------------------------

# MAKE SURE imagemagick is installed and on your PATH before enabling imagemagick. In a terminal, run:
# convert -version
imagemagick:
enabled: true # enables responsive images for your site (recommended, see https://github.com/alshedivat/al-folio/issues/537)
widths:
Expand All @@ -341,8 +343,9 @@ imagemagick:
- ".jpeg"
- ".png"
- ".tiff"
- ".gif"
output_formats:
webp: "-resize 800x"
webp: "-quality 85 -strip"

# -----------------------------------------------------------------------------
# Jekyll Diagrams
Expand Down
23 changes: 11 additions & 12 deletions _includes/figure.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
{%- assign img_path = include.path | remove: ".jpg" | remove: ".jpeg" | remove: ".png" | remove: ".tiff" -%}
{%- assign img_path = include.path | remove: ".jpg" | remove: ".jpeg" | remove: ".png" | remove: ".tiff" | remove: ".gif" -%}

<figure>

<picture>
<!-- Auto scaling with imagemagick -->
<!-- See https://www.debugbear.com/blog/responsive-images#w-descriptors-and-the-sizes-attribute and
https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images for info on defining 'sizes' for responsive images-->
{% if site.imagemagick.enabled %}
{% for i in site.imagemagick.widths -%}
<source
class="responsive-img-srcset"
media="(max-width: {{ i }}px)"
srcset="{{ img_path | relative_url }}-{{ i }}.webp"
/>
{% endfor -%}
<source class = "responsive-img-srcset" srcset="
{% for i in site.imagemagick.widths -%}
{{ img_path | relative_url }}-{{ i }}.webp {{i}}w,
{% endfor -%}"
{% if include.sizes %} sizes="{{include.sizes}}" {% else %} sizes="95vw" {% endif %}
type="image/webp"/>
{% endif %}

<!-- Fallback to the original file -->
<img
src="{% if include.cache_bust %}{{ include.path | relative_url | bust_file_cache }}{% else %}{{ include.path | relative_url }}{% endif %}"
{% if include.class %}class="{{ include.class }}"{% endif %}
{% if include.width %}width="{{ include.width }}"{% else %}width="auto"{% endif %}
{% if include.width %}width="{{ include.width }}"{% else %}width="100%"{% endif %}
alshedivat marked this conversation as resolved.
Show resolved Hide resolved
{% if include.height %}height="{{ include.height }}"{% else %}height="auto"{% endif %}
{% if include.min-width %}min-width="{{ include.min-width }}"{% endif %}
{% if include.min-height %}min-height="{{ include.min-height }}"{% endif %}
Expand Down
1 change: 1 addition & 0 deletions _includes/projects.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{%- if project.img %}
{%- include figure.html
path=project.img
sizes = "250px"
alt="project thumbnail" -%}
{%- endif %}
<div class="card-body">
Expand Down
2 changes: 1 addition & 1 deletion _includes/projects_horizontal.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="row g-0">
{%- if project.img -%}
<div class="card-img col-md-6">
{% include figure.html path=project.img alt="project thumbnail" %}
{% include figure.html path=project.img sizes="(min-width: 768px) 156px, 50vw" alt="project thumbnail" %}
</div>
<div class="col-md-6">
{%- else -%}
Expand Down
12 changes: 8 additions & 4 deletions _layouts/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ <h1 class="post-title">
{%- assign profile_image_class = "img-fluid z-depth-1 rounded" -%}
{% endif %}

{% capture sizes %}(min-width: {{site.max_width}}) {{ site.max_width | minus: 30 | times: 0.3}}px, (min-width: 576px) 30vw, 95vw"{% endcapture %}

{% include figure.html
path=profile_image_path
class=profile_image_class
alt=page.profile.image
cache_bust=true -%}
path = profile_image_path
class = profile_image_class
sizes = sizes
alt = page.profile.image
cache_bust = true
%}
{% endif -%}
{%- if page.profile.more_info %}
<div class="more-info">
Expand Down
1 change: 1 addition & 0 deletions _layouts/bib.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{%- assign entry_path = entry.preview | prepend: '/assets/img/publication_preview/' -%}
{% include figure.html
path=entry_path
sizes = "200px"
class="preview z-depth-1 rounded"
zoomable=false
alt=entry.preview -%}
Expand Down
10 changes: 7 additions & 3 deletions _layouts/profiles.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
{%- assign profile_image_class = "img-fluid z-depth-1 rounded" -%}
{% endif %}

{% capture sizes %}(min-width: {{site.max_width}}) {{ site.max_width | minus: 30 | times: 0.3}}px, (min-width: 576px) 30vw, 95vw"{% endcapture %}

{% include figure.html
path=profile_image_path
class=profile_image_class
alt=profile.image -%}
path = profile_image_path
class = profile_image_class
sizes = sizes
alt = profile.image
%}
{% endif -%}
{%- if profile.more_info %}
<div class="more-info">
Expand Down