{"record":{"id":"a702fdc10f1ec62e","repo":"gohugoio/hugo","slug":"final-image-width-will-be-less-than-1-pixel-check","errorCode":null,"errorMessage":"final image width will be less than 1 pixel: check padding values","messagePattern":"final image width will be less than 1 pixel: check padding values","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"resources/images/padding.go","lineNumber":36,"sourceCode":"\t\"image/color\"\n\t\"image/draw\"\n\n\t\"github.com/gohugoio/gift\"\n)\n\nvar _ gift.Filter = (*paddingFilter)(nil)\n\ntype paddingFilter struct {\n\ttop, right, bottom, left int\n\tccolor                   color.Color // canvas color\n}\n\nfunc (f paddingFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {\n\tw := src.Bounds().Dx() + f.left + f.right\n\th := src.Bounds().Dy() + f.top + f.bottom\n\n\tif w < 1 {\n\t\tpanic(\"final image width will be less than 1 pixel: check padding values\")\n\t}\n\tif h < 1 {\n\t\tpanic(\"final image height will be less than 1 pixel: check padding values\")\n\t}\n\n\ti := image.NewRGBA(image.Rect(0, 0, w, h))\n\tdraw.Draw(i, i.Bounds(), image.NewUniform(f.ccolor), image.Point{}, draw.Src)\n\tgift.New().Draw(dst, i)\n\tgift.New().DrawAt(dst, src, image.Pt(f.left, f.top), gift.OverOperator)\n}\n\nfunc (f paddingFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {\n\treturn image.Rect(0, 0, srcBounds.Dx()+f.left+f.right, srcBounds.Dy()+f.top+f.bottom)\n}\n","sourceCodeStart":18,"sourceCodeEnd":51,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/resources/images/padding.go#L18-L51","documentation":"Thrown by the padding filter's Draw when the resulting canvas width is less than 1 pixel (padding.go:32-37). Width = source width + left + right; with negative padding (cropping) that exceeds the source width, the canvas becomes non-positive and the filter aborts.","triggerScenarios":"Passing negative padding whose magnitude exceeds the image width, e.g. a 50px-wide image with left=-100, right=-100. Negative values crop; over-cropping destroys the canvas.","commonSituations":"Using padding to crop dynamically without clamping to image dimensions; applying a fixed negative padding to a set of images of varying widths.","solutions":["Clamp negative padding so |left + right| < source width (and leave at least 1px).","Use images.Crop for intentional cropping instead of negative padding.","Validate image dimensions before applying aggressive negative padding."],"exampleFix":"// before\n{{ $filters = $filters | append (images.Padding -100 -100) }}\n// after\n{{ $filters = $filters | append (images.Crop \"20x20 Center\") }}","handlingStrategy":"validation","validationCode":"{{ $w := $img.Width }}\n{{ if le (add $w (add $left $right)) 0 }}\n  {{ errorf \"negative padding exceeds image width %d\" $w }}\n{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer images.Crop for cropping rather than negative padding.","Clamp negative padding so left+right > -width.","Validate source dimensions before aggressive padding."],"tags":["image-filter","padding","crop","validation","hugo"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}