Skip to content

Commit

Permalink
img shortcode: Extend access to resize_image's quality arg
Browse files Browse the repository at this point in the history
  • Loading branch information
justint committed Oct 15, 2021
1 parent 1f2859e commit b9e74e7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -205,7 +205,7 @@ linkedin = "papayatiliqua"
Included with Papaya is a shortcode for embedding images into your posts:

```
img(path, alt, caption, class, extended_width_pct)
img(path, alt, caption, class, extended_width_pct, quality)
```

### Arguments
Expand All @@ -214,6 +214,7 @@ img(path, alt, caption, class, extended_width_pct)
- `alt`: _(optional)_ The alternate text for the image.
- `caption`: _(optional)_ A caption for the image. Text/HTML/Tera templates supported.
- `class`: _(optional)_ Any CSS classes to assign to the image. Multiple classes should be separated with a space (`" "`).
- `quality`: _(optional)_ JPEG or WebP quality of the image, in percent. Only used when encoding JPEGs or WebPs; default value is `90`.
- `extended_width_pct`: _(optional)_ The percentage by which the image's width should be expanded past it's default figure width, up to maximum configured pixel width.

Range is `0.0-1.0`, or `-1` for document width.
Expand Down
11 changes: 8 additions & 3 deletions templates/shortcodes/img.html
@@ -1,11 +1,16 @@
{# {{ img(path="", alt="", class="", caption="", extended_width_pct=1.3) }} #}
{# {{ img(path="", alt="", class="", caption="", extended_width_pct=1.3, quality=90) }} #}

{# container_width = (pixel size, px) * (.container's max-width) #}
{% set container_width = 16 * 42 %}

{# figure_width = container_width - (figure margin_blocks) #}
{% set figure_width = container_width - (40 * 2) %}

{% set image_quality = 90 %}
{% if quality %}
{% set image_quality = quality %}
{% endif %}

{% if extended_width_pct %}
{% if extended_width_pct == -1 %}
{# adjust extended width to container width #}
Expand All @@ -16,9 +21,9 @@
{% if extended_width > config.extra.images.max_width %}
{% set extended_width = config.extra.images.max_width %}
{% endif %}
{% set resized_image = resize_image(path=path, width=extended_width, op="fit_width") %}
{% set resized_image = resize_image(path=path, width=extended_width, op="fit_width", quality=image_quality) %}
{% else %}
{% set resized_image = resize_image(path=path, width=figure_width, op="fit_width") %}
{% set resized_image = resize_image(path=path, width=figure_width, op="fit_width", quality=image_quality) %}
{% endif %}
<figure {% if extended_width_pct %}class="extended-figure"{% endif %}>
<img src="{{ resized_image.url }}" class="{% if class %}{{class}}{% endif %}" {% if alt %}alt="{{alt}}"{% endif %}/>
Expand Down

0 comments on commit b9e74e7

Please sign in to comment.