Automatic image thumbnails in Hugo from static directory
All notes in this series:
- Automating hugo development with npm scripts
- normalize-scss with hugo
- Automatic image thumbnails in Hugo from static directory
- Escaping Hugo shortcodes within Hugo markdown
- Hugo tag and category pages
- Bind hugo to localhost for development
- Hugo 0.37 does not support h6 markdown heading
- Install Hugo testing distribution on Debian
- Hugo anchors next to headers
- Hugo: Migrating from Pygments to Chroma
- Hugo: Global resources
- Hugo: How to create a post series
Hugo v0.32 introduced image processing, which can be used to resize, fit or fill images. This can be useful to, for example, automatically create a low resolution thumbnail from a high resolution image. Hugo v0.34 (latest at time of writing) modified the API for image processing.
Using the image processing in Hugo v0.34, my goal is this:
- Store my full resolution “original” images in the static directory (
/static/img/
) - Use Hugo to automatically generate low resolution “thumbnail” images
- Use a shortcode to include the thumbnail image in markdown as a link to the original image
Shortcodes §
Hugo image processing can be used directly in Hugo templates, but there is no syntax for using it within markdown. The solution is to use Hugo shortcodes.
Shortcodes let us include a Hugo template with a markdown file. For example, using the following post and shortcode…
…will cause Hugo to render a web page as follows:
Page Resources §
The Hugo v0.32 release notes include an example shortcode which addresses goals 2 and 3 I outlined above, but does not address goal 1. Further, I wanted to write the shortcode myself to help me better understand how everything fits together.
Goal 1 is not addressed by the aforementioned example because it relies upon including your images within your /content/
directory rather than within your /static/
directory. Using the static directory is desirable for me because I want the option to be able to reference the same image from different posts, rather than assuming a one-to-one relationship between each image and a post.
We can use .Page.Resources
within a template to find resources (e.g. images) within that post. For example, given the following directory structure:
If we create a shortcode to list all image resources:
And then use this shortcode within our example post:
We get the following page rendered:
However it appears there is no way to get .Page.Resources
to search in our /static/img/
directory. There is a proposal to add a .Site.Resources
but as of Hugo v0.34 it is just not possible.
So there you have it. Not currently possible.
Or is it?
Hacky solution §
Alright…
Let’s alter our structure from earlier to create a new “fakestatic” section with a “fakepost” which contains all our static content:
Then in fakestatic/fakepost/index.md
we have:
If we take our example shortcode from earlier and take advantage of the GetPage
function, we can list the images from “fakepost” in any other post.
The above shows that we can access the resources of one post from another. Next, we create a shortcode that accepts the image name as an argument and displays the said image:
We can then use the above to include any arbitrary image from /content/fakestatic/fakepost/images/
in any markdown post:
Adding the thumbnail §
Adding a caption and some style §
We can make things a bit more interesting by adding an optional caption and styling.
To show the above in action:
Bonus: getting nicer URLs §
To store the resources with nicer URLs, we can move things around at build time. For example, to store full size images within public/img/full/
rather than public/fake-post/images/
, and to store thumbnails within public/img/thumb/
, we can create the following script:
This requires fs-extra
to be added to the devDependencies
, e.g.:
Then add a call to fakestatic.js
within the build command, e.g.:
Then when you run the build command, it will move the “global” resources into the public/img/
directory, and they will be accessible at /img/
on the deployed website.
All notes in this series:
- Automating hugo development with npm scripts
- normalize-scss with hugo
- Automatic image thumbnails in Hugo from static directory
- Escaping Hugo shortcodes within Hugo markdown
- Hugo tag and category pages
- Bind hugo to localhost for development
- Hugo 0.37 does not support h6 markdown heading
- Install Hugo testing distribution on Debian
- Hugo anchors next to headers
- Hugo: Migrating from Pygments to Chroma
- Hugo: Global resources
- Hugo: How to create a post series