Big Photos, Small Pages: Image Conversion in the Build
Phone photos arrive far larger than any page needs. Every build container has libvips, so resizing and WebP conversion happen as the site builds.
A photo from a modern phone is thousands of pixels wide. A hero image on a web page rarely needs to be more than about 1,600 to 2,000 pixels across. Put the original on the page and every visitor downloads the whole file, which on a weak mobile signal is the wait they notice.
iPhones can also save photos as HEIF, Apple’s space-saving format, when the camera is set to High Efficiency (Apple). That suits the phone’s storage. It is not what you want to serve on a website.
Converting as part of the build
Every build container we run includes libvips and Python 3 (build configuration). libvips resizes and converts in one step:
vipsthumbnail hero.avif --size 2000x2000 -o hero.webp[Q=80]
That fits the image inside 2,000 by 2,000 pixels, writes it as WebP at quality 80, and never makes an image bigger than it was. libvips streams the picture rather than loading it whole, so it stays inside the container’s 1 GB of memory even for large photos.
WebP is the usual target. Google measured lossy WebP files at 25 to 34% smaller than comparable JPEGs at the same quality (Google). The same tools convert AVIF and HEIF sources to WebP.
Put the conversion in a script in your repository and run it from your build command, before Hugo or Jekyll. The build images are Alpine-based and have no bash, so write the script for sh. Make it skip images it has already converted, so repeat builds stay well inside the ten-minute limit.
Hugo’s own image processing
Hugo can resize images itself, and its WebP encoding is slow on large sources because it runs in a sandboxed encoder. One oversized original, resized into several widths, can push a build past the ten-minute timeout. Our build log warns you when it spots an oversized source image and names the file and its dimensions.
Two habits keep Hugo quick. Shrink the original you commit to roughly the largest size you display, and generate only the srcset widths your pages use.
For an image-heavy site
The container’s /tmp is a small RAM disk, 256 MB. Point TMPDIR at your workspace and the scratch files have room:
export TMPDIR="$PWD/.buildtmp" && mkdir -p "$TMPDIR" && sh scripts/prebuild-images.sh && hugo --minify
We have looked before at why static pages arrive faster, and the one thing a static site can’t fix on its own: an oversized photo is as slow here as anywhere else. A few lines in the build stop you shipping one.
Ready to Deploy?
Join Jamstack developers across the UK hosting their static sites with privacy and performance built in. Hosting is free for good, no credit card needed, with Plus features when you want them.
