Dynamic OG Images in Hugo

Hugo is popular for docs and blogs because it is fast and has no JavaScript toolchain. That also means the Satori/JSX approach used in the Node ecosystem doesn't apply. Hugo has its own tool for this: image processing with images.Text, which can draw text onto a base image at build time.

This guide covers Hugo's built-in Open Graph template, the DIY images.Text approach, its limits, and the one-URL alternative with Unfurlkit.

Version note: Checked against the Hugo docs in Oct 2026. images.Text options: alignx was added in v0.141.0 and aligny in v0.147.0. Current docs place partials, including overrides of the embedded templates, in layouts/_partials/, which is part of the newer template system (introduced in v0.146.0). Check the docs for your version: https://gohugo.io/functions/images/text/ and https://gohugo.io/templates/embedded/#open-graph.

Step 0: The basics

Every OG image should be:

  • 1200×630 px
  • Linked with an absolute URL. Set baseURL in your site config and use .Permalink, not .RelPermalink.
  • Paired with twitter:card set to summary_large_image
  • Publicly reachable

Step 1: Hugo's built-in Open Graph template

Hugo ships embedded templates that output og: and twitter: tags from your front matter. In your <head> partial:

{{/* layouts/partials/head.html  (layouts/_partials/head.html on Hugo 0.146+) */}}
{{ partial "opengraph.html" . }}
{{ partial "twitter_cards.html" . }}

Hugo's docs now show the embedded templates called with partial, as above. On older Hugo versions, use the internal template syntax:

{{ template "_internal/opengraph.html" . }}
{{ template "_internal/twitter_cards.html" . }}

These templates pick up images from the images front matter parameter. If that isn't set, they look for page resources matching *feature*, then *cover* or *thumbnail*, and finally the first entry in the site's params.images. The X template sets twitter:card to summary_large_image when it finds an image and to summary when it doesn't (per the Hugo docs). That's fine if you make an image for each post by hand. For generated images, you'll write your own partial.

Step 2 (DIY): Generate images with images.Text

The idea: start with a 1200×630 background image, draw the page title on it at build time, and output the result's permalink.

Assets

assets/
  og/
    base.png          ← 1200×630 branded background
    Inter-Bold.ttf    ← font for the title

The partial

{{/* layouts/partials/og-image.html */}}
{{- $base := resources.Get "og/base.png" -}}
{{- $font := resources.Get "og/Inter-Bold.ttf" -}}
{{- $title := .Title | truncate 80 -}}

{{- $img := $base | images.Filter (images.Text $title (dict
      "color" "#ffffff"
      "size" 64
      "linespacing" 12
      "x" 80
      "y" 220
      "font" $font
    )) -}}

{{- $img = $img | resources.Copy (printf "og/%s.png" (.File.ContentBaseName | default (urlize .Title))) -}}
{{- return $img.Permalink -}}

Use it in your head partial:

{{/* layouts/partials/head.html */}}
{{- $ogImage := partial "og-image.html" . -}}
<meta property="og:title" content="{{ .Title }}" />
<meta property="og:description" content="{{ .Description | default .Summary | plainify }}" />
<meta property="og:url" content="{{ .Permalink }}" />
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" />
<meta property="og:image" content="{{ $ogImage }}" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="{{ $ogImage }}" />

Notes:

  • return in a partial is long-standing Hugo syntax. On very old versions, inline the logic instead.
  • .Permalink on a resource is absolute as long as baseURL is set correctly. If your OG URLs come out as http://localhost:1313/... in production, your build is using the wrong baseURL.
  • Hugo caches processed images in resources/_gen. Commit that folder or cache it in CI to speed up rebuilds.

Wrapping long titles

The images.Text options are color, font, size (default 20), linespacing (default 2), x, y, alignx (v0.141+), and aligny (v0.147+). There is no option to set a box or maximum width, so you can't control where lines wrap. Positioning is by pixel offsets from the top-left corner. Common workarounds for long titles:

  • Truncate titles (as above) and pick a font size that fits your longest realistic title.
  • Split the title into lines yourself with split/seq and call images.Text once per line with an increasing y.
  • Use a smaller size when len $title is above a threshold.

On v0.147+, you can center a block by setting alignx/aligny to center with x/y at half the image's width and height. This is the pattern in Hugo's own images.Text example.

A note on fonts

If you don't pass font, Hugo uses Go Regular. The font option accepts a global resource, a page resource, or a remote resource (resources.GetRemote), so you can pull a Google Font at build time instead of committing the file. If you do that, wrap the call in try so a network failure stops the build with a clear error, as the docs show, rather than leaving you with a blank image.

The honest trade-offs of DIY

The images.Text approach is neat: no Node, no external service, and everything happens inside hugo build. It is also the most limited of the three frameworks we've covered:

  • Text layout is manual. There's no flexbox and no width box, so you position text by pixel coordinates.
  • One text style per call. Mixing weights or colors (for example, a bold title and a lighter author line) means several calls with hand-tuned coordinates.
  • Design changes mean editing a PNG. The template is a background image plus coordinates.
  • Build time grows with page count. Hugo is fast and caches results, but cold builds of large sites will take longer.
  • Per-language and emoji support depends on the font you supply.

For a simple blog these limits are fine. For a docs site with thousands of pages, long titles, and several languages, they start to cost time.

Step 2 (alternative): One URL with Unfurlkit

Replace the image partial with a URL. There's no image processing and nothing to cache:

{{/* layouts/partials/og-image.html */}}
{{- $params := querify "title" (.Title | default site.Title) "theme" "dark" -}}
{{- return printf "https://unfurlkit.com/v1/basic.png?%s" $params -}}

querify URL-encodes the parameters for you. The head partial above doesn't change. It still outputs og:image and twitter:image, now pointing at Unfurlkit:

<meta property="og:image" content="https://unfurlkit.com/v1/basic.png?theme=dark&title=Getting%20Started" />

What changes:

  • Real text layout. Wrapping and styling are handled by the template, not by pixel coordinates. (On Pro: custom fonts (Inter, Roboto, Playfair Display, JetBrains Mono) and logo.)
  • Nothing added to your build. No resources/_gen growth and no extra build time per page.
  • Absolute, correctly sized images by default.
  • One pattern across stacks. Your Next.js app and Astro blog can use the same templates.

Every image URL needs your key. A Free key gets 10 renders in total (no monthly reset), with a small "unfurlkit" watermark, and needs no card. Pro, $9/mo or $90/yr (10k renders/month), removes it and adds custom fonts (Inter, Roboto, Playfair Display, JetBrains Mono) and logo.

Which should you use?

Situation Recommendation
Hand-made cover image per post Built-in opengraph.html + front matter images
Simple, short titles, and you want zero dependencies images.Text partial
Long titles, many pages, richer design Unfurlkit

Debugging checklist

  1. Run hugo and check the page source in public/. Is og:image present and absolute?
  2. If you see localhost in URLs, check baseURL and any --baseURL flags in CI.
  3. Open the image URL. It should return 200 image/png at 1200×630.
  4. Confirm twitter:card is summary_large_image.
  5. Platforms cache previews. Re-scrape with LinkedIn Post Inspector or Facebook Sharing Debugger, or version the URL.

The free Free OG Image Checker renders your page as X, LinkedIn, Facebook, Slack, Discord, and iMessage would show it, and flags what to fix.

Summary

Hugo gives you two native options: the embedded Open Graph templates for hand-made images, and images.Text for simple generated ones. Both work without extra tools. When you need better text layout or don't want image generation in your build, a single Unfurlkit URL in a partial does the job.

FAQ

Which languages and scripts are supported? Right-to-left scripts such as Arabic and Hebrew aren't supported yet at launch.

Can I let authors override the generated image? Yes. In the partial, check front matter first:

{{- with .Params.og_image -}}
  {{- return (. | absURL) -}}
{{- end -}}

Put this before the generation logic. Authors who set og_image in front matter get their own image, and everyone else gets the generated one.

Which Hugo edition do I need? The images.Text docs don't list an edition requirement. If your build fails on image processing, check the installation docs for your Hugo version.

What image format should I output? PNG keeps text sharp and works everywhere. Don't use WebP or SVG for og:image, because platform support is inconsistent.

My section and home pages have no image. What should I do? .Title exists on list pages too, so the same partial works there. Otherwise, fall back to a static static/og/default.png linked with absURL.

Why do I see the old image on LinkedIn after changing it? LinkedIn, Facebook, and X cache previews. Use LinkedIn's Post Inspector or Facebook's Sharing Debugger to re-scrape. For X, or as a general fix, change the image URL. With images.Text you can include a short hash of the title in the filename. With Unfurlkit, any change to the query string produces a new URL.

Will the Unfurlkit URL leak anything sensitive? Only what you put in it, which is normally the public page title. Don't put drafts or private data in query parameters. Pro URLs also carry your public key k and a signature s. Your secret key stays on the machine that builds the site. See Signing Pro URLs.

Does this work with Hugo themes? Most themes include a head partial. Override it by creating a file with the same path in your project's layouts/ directory. Hugo's lookup order prefers project files over theme files, so you don't need to fork the theme.

See Unfurlkit pricing · Check any page with the Free OG Image Checker.

Updated 2026-10-21

Try it freePricing