Help:Shortcode Img

Display zoomable images, optionally in a figure.

Img
Shortcode
Description
TypeHTML Shortcode
Nested?false
Shortcodeimg
Return TypeHTML
Short Description
Display an image, which is zoomable by default, optionally in an HTML figure.
Development
MaintainerMichael Sasser

It is possible to use the file formats jpeg, png, tiff, bmp, gif and svg. Images are lazyloaded, blurred up, and responsive. They need to be place in a page bundle , for example, like this site bundle :

# tree of ./content/en/docs/tutorial/create_account
...
├── tutorial/
│   ├── create_account/
│   │   ├── index.md
│   │   ├── element-create-account-recaptcha.png
│   │   ├── element-create-account-accept-terms.png
│   │   ├── element-create-account-email.png
│   │   ├── element-create-account-filled.png
│   │   ├── element-create-account-github-sso.png
│   │   ├── element-create-account-google-sso.png
│   │   ├── element-create-account-registration-successful.png
│   │   ├── element-create-account-verification-mail.png
│   │   ├── element-create-account.png
│   │   ├── element-initial-view.png
│   │   ├── element-sign-in-filled.png
│   │   ├── element-sign-in.png
│   │   └── element-welcome.png
│   └── _index.md
└── _index.md

Parameters

The img shortcode has the following parameters:

ParameterOptionalDescription
srcNoThe relative path to the image
widthYesThe width of the image
heightYesThe height of the image
figure_classYesAdd classes to the figure
img_classYesAdd classes to the HTML img
altYesAdd an alt string to the HTML img
captionYesAdd an figure to the image with an caption

Examples

Front Matter

Remember to add every image you use to the images variable in the document header.
For example: images: ["foo.svg", "bar.jpeg"]
{{< img src="image.svg" width="50" >}}
{{< img src="image.svg" width="100" alt="Our logo" caption="<em>The Matrix+Python Logo</em>" >}}
{{< img src="image.svg" >}}
Rendered
Our logo
The Matrix+Python Logo

Code

Below you find the implementation of the shortcode.

HTML

Defined in layouts/shortcodes/img.html.

This code is licensed under the MIT license.
<!--

  Img Shortcode
  =============

  Contributors: Michael Sasser
  Maintainer: Michael Sasser
  License: MIT
  Version: 1
  Child Shortcodes: None
  Parent Shortcodes: None

  LAYOUT: /layouts/shortcodes/img.html
  STYLE:  /assets/scss/components/_shortcode_img.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Img.md
  LIB:    medium-zoom
  SCRIPT: /assets/js/medium-zoom.js

  Description
  -----------

  Use the img shortcode to create an image. Optionally with an HTML figure.

  Changelog
  =========

  Version 1
  ---------

  Initial release

  Version 2
  ---------

  Rewrite to add more class hooks and make use of the medium-zoom library and
  allow using vector images.

-->

{{- $image_src := .Get "src" -}}
{{- $image := .Page.Resources.GetMatch (printf "*%s*" $image_src) -}}
{{- $is_vector := eq (path.Ext $image) ".svg" -}}

{{- if eq $image nil -}}
  {{ errorf "Failed to process img shortcode: %s. %s does not exist next to \"%s\"." $.Position $image_src .Page.Path }}
{{- end -}}

{{/*
<!--
if (isset $image "Width")  produces an non fatal error in the new version of
hugo. Basically every image excep vector images should have a slice. So the
workaround is checking, if the image is not an vector image.
Same for the other two: $image.Height and $image.Resize
-->
*/}}

{{- $width := false -}}
{{- if (not $is_vector) -}}
  {{- $width = $image.Width -}}
{{- end -}}
{{- with (.Get "width") -}}
  {{- $width = . -}}
{{- end -}}

{{- $height := false -}}
{{- if (not $is_vector) -}}
  {{- $height = .Get "height" | default $image.Height -}}
{{- end -}}
{{- with (.Get "height") -}}
  {{- $height = . -}}
{{- end -}}

{{- $lqip := false -}}
{{- if (not $is_vector) -}}
  {{- $w := printf "%dx webp q%d %s %s" $.Site.Params.lqipWidth $.Site.Params.quality $.Site.Params.hint $.Site.Params.resampleFilter -}}
  {{- $lqip = $image.Resize $w -}}
{{- end -}}

{{ $imgSrc := "" -}}
{{ $imgSrcSet := slice -}}

{{- if (not $is_vector) -}}
  {{ $widths := $.Site.Params.landscapePhotoWidths -}}
  {{ if gt $image.Height $image.Width -}}
    {{ $widths = $.Site.Params.portraitPhotoWidths -}}
  {{ end -}}

  {{ range $widths -}}
    {{ $srcUrl := (printf "%dx webp q%d %s %s" . $.Site.Params.quality $.Site.Params.hint $.Site.Params.resampleFilter | $image.Resize).Permalink -}}
    {{ if eq $imgSrc "" -}}{{ $imgSrc = $srcUrl -}}{{ end -}}
    {{ $imgSrcSet = $imgSrcSet | append (printf "%s %dw" $srcUrl .) -}}
  {{ end -}}
  {{ $imgSrcSet = (delimit $imgSrcSet ",") -}}
{{ end -}}

<figure {{ with .Get "figure_class" -}}class="{{- . -}}"{{- end -}}>
  {{- if and (eq .Site.Params.options.lazySizes true) (not $is_vector) -}}
  <img
    class="img-fluid lazyload blur-up rounded mx-auto d-block {{ with .Get "img_class" -}}{{- . -}}{{- end -}}"
    id="zoom-default"
    data-sizes="auto"
    {{ with $lqip -}}src="{{- .RelPermalink -}}"{{- end }}
    data-srcset="{{ $imgSrcSet }}"
    {{ with $width -}}width="{{- . -}}"{{- end }}
    {{ with $height -}}height="{{- . -}}"{{- end }}
    {{ with .Get "alt" }}alt="{{.}}"{{ end }}
  >
  </img>
  {{- else -}}
    <img
      class="img-fluid zoom-default rounded mx-auto d-block {{ with .Get "img_class" -}}{{- . -}}{{- end -}}"
      id="zoom-default"
      sizes="100vw"
      {{ with $imgSrcSet -}}srcset="{{- . -}}"{{- end }}
      src="{{ $image.RelPermalink }}"
      {{ with $width -}}width="{{- . -}}"{{- end }}
      {{ with $height -}}height="{{- . -}}"{{- end }}
      {{ with .Get "alt" -}}alt="{{- . -}}"{{- end }}
  >
  </img>
  {{ end -}}
  <noscript>
    <img
      class="img-fluid rounded mx-auto d-block {{ with .Get "img_class" -}}{{- . -}}{{- end -}}"
      sizes="100vw"
      srcset="{{ $imgSrcSet }}"
      src="{{ $image.RelPermalink }}"
      {{ with $width -}}width="{{- . -}}"{{- end }}
      {{ with $height -}}height="{{- . -}}"{{- end }}
      {{ with .Get "alt" -}}alt="{{- . -}}"{{- end }}
    >
    </img>
</noscript>
{{- with .Get "caption" -}}<figcaption class="figure-caption">{{- . | safeHTML -}}</figcaption>{{- end -}}
</figure>

Download

Copy the source code above or use the download link below to use this file on your website according to the license.

  • Accordion Item Shortcode
    img.html4.04 KB
  • md5   5fbeab56eab7d991f1f972bfc8792188
  • sha1   07f51a9c300ab038e152345792c23a9ad09ab45d
  • sha256   3815fe0f35912dd10681739d4dcf5007a285c7d449e454b1cb77ec23f106550b

SASS

Defined in: /assets/scss/components/_shortcode_img.scss

This code is licensed under the MIT license.
/*

  Img Shortcode
  =============

  Contributors: Michael Sasser
  Maintainer: Michael Sasser
  License: MIT
  Version: 1
  Child Shortcodes: None
  Parent Shortcodes: None

  LAYOUT: /layouts/shortcodes/img.html
  STYLE:  /assets/scss/components/_shortcode_img.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Img.md
  LIB:    medium-zoom
  SCRIPT: /assets/js/medium-zoom.js

  Figure Shortcode
  ================

  LAYOUT: /layouts/shortcodes/figure.html
  STYLE:  /assets/scss/components/_shortcode_img.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Figure.md

*/

figure {
  margin: 2rem 0;
}

.figure-caption {
  margin: 0.25rem 0 0.75rem;
  text-align: center;
}

figure.wide {
  margin: 2rem -1.5rem;
}

figure.wide .figure-caption {
  margin: 0.25rem 1.5rem 0.75rem;
}

@include media-breakpoint-up(md) {
  figure.wide {
    margin: 2rem -2.5rem;
  }

  figure.wide .figure-caption {
    margin: 0.25rem 2.5rem 0.75rem;
  }
}

@include media-breakpoint-up(lg) {
  figure.wide {
    margin: 2rem -5rem;
  }

  figure.wide .figure-caption {
    margin: 0.25rem 5rem 0.75rem;
  }
}

.blur-up {
  filter: blur(5px);
}

.blur-up.lazyloaded {
  filter: unset;
}

.img-simple {
  margin-top: 0.375rem;
  margin-bottom: 1.25rem;
}

Download

Copy the source code above or use the download link below to use this file on your website according to the license.

  • Img Style
    _shortcode_img.scss1.25 KB
  • md5   f22b004f80d1f3a29c771f73e8f21892
  • sha1   11cc59660c112bf81eb915429c74668ce2238e6a
  • sha256   38a4e5c460332cfd54b485ca2bacd7d2f21d2eea8f663bee6a6a2bb82e96ae2b

JavaScript

Defined in: /assets/js/medium-zoom.js
Uses: medium-zoom (Package: NPM , License: MIT )

This code is licensed under the MIT license.
/*

  Img Shortcode
  =============

  Contributors: Michael Sasser
  Maintainer: Michael Sasser
  License: MIT
  Version: 1
  Child Shortcodes: None
  Parent Shortcodes: None

  LAYOUT: /layouts/shortcodes/img.html
  STYLE:  /assets/scss/components/_shortcode_img.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Img.md
  LIB:    medium-zoom
  SCRIPT: /assets/js/medium-zoom.js

*/

import mediumZoom from 'medium-zoom'


document.addEventListener('lazybeforeunveil', () => {
    mediumZoom('#zoom-default', {
        margin: 70,
        background: 'rgba(00, 00, 00, .3)',
        scrollOffset: 0,
    })
})

Download

Copy the source code above or use the download link below to use this file on your website according to the license.

  • Medium-Zoom Script
    medium-zoom.js616.00 Bytes
  • md5   4861cb197ac8ff9cb4055d638a27ae34
  • sha1   b6b6f5f720d866ea72f905af6bf7601d16d90ed3
  • sha256   283bad47d45869bfb210077e3fb9f8bb14e0c1d81f990c09b5826d245077925d
Top