gohugoio/hugo · error

must provide Width and Height

Error message

must provide Width and Height

What it means

The crop, fill, and fit image actions must know the exact target box, so Hugo requires both width and height to be non-zero after parsing the option string. A missing dimension leaves it at 0, which these actions cannot work with.

Source

Thrown at resources/images/config.go:321

				if len(widthHeight) == 2 {
					second := widthHeight[1]
					if second != "" {
						c.Height, err = strconv.Atoi(second)
						if err != nil {
							return c, err
						}
					}
				}
			} else {
				return c, errors.New("invalid image dimensions")
			}
		}
	}

	switch c.Action {
	case ActionCrop, ActionFill, ActionFit:
		if c.Width == 0 || c.Height == 0 {
			return c, errors.New("must provide Width and Height")
		}
	case ActionResize:
		if c.Width == 0 && c.Height == 0 {
			return c, errors.New("must provide Width or Height")
		}
	default:
		if c.Width != 0 || c.Height != 0 {
			return c, errors.New("width or height are not supported for this action")
		}
	}

	if err := c.init(defaults.Config, sourceFormat); err != nil {
		return c, err
	}

	if mainImageVersionNumber > 0 {
		options = append(options, strconv.Itoa(mainImageVersionNumber))
	}

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Provide both dimensions: {{ $img.Fill "600x400" }}.
  2. If you only want to constrain one dimension with aspect preserved, use .Resize instead of Fill/Fit/Crop.
  3. When dimensions come from variables, validate both are > 0 before calling the method.

Example fix

<!-- before -->
{{ $img.Fill "600x" }}
<!-- after -->
{{ $img.Fill "600x400" }}
Defensive patterns

Strategy: validation

Validate before calling

if action == "crop" || action == "fill" || action == "fit" {
    if w == 0 || h == 0 {
        return errors.New("crop/fill/fit require both width and height")
    }
}

Prevention

When it happens

Trigger: {{ $img.Fill "600x" }}, {{ $img.Crop "x400" }}, {{ $img.Fit "600" }} (no 'x' at all leaves both 0), or an images.Process call with action crop/fill/fit and an incomplete size spec.

Common situations: Habitually using the one-dimension form that works for Resize with Fill/Fit/Crop; templates where one dimension variable is empty/zero; migrating from .Resize to .Fill without adding the second dimension.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/fc75e83b868a6e5f.json. Report an issue: GitHub ↗.