Handle PNG EXIF and distinguish transient encoder crashes from invalid images

This commit is contained in:
2026-11-21 00:58:18 +03:00
parent a9203f677d
commit 8bd25a1321
13 changed files with 690 additions and 85 deletions
+9 -5
View File
@@ -47,19 +47,23 @@ func Transform(input, dir string, versions ...int) error {
if e != nil {
return e
}
c, _, e := image.DecodeConfig(f)
c, format, e := image.DecodeConfig(f)
f.Close()
if e != nil || c.Width < 1 || c.Height < 1 || int64(c.Width)*int64(c.Height) > maxPixels {
return fmt.Errorf("invalid_image")
return ErrInvalidImage
}
img, e := imaging.Open(input, imaging.AutoOrientation(true))
if e != nil {
return fmt.Errorf("invalid_image")
return ErrInvalidImage
}
bounds := img.Bounds()
var oriented image.Image = img
if format == "png" {
oriented = orientPNG(img, pngOrientation(input))
}
bounds := oriented.Bounds()
white := image.NewNRGBA(bounds)
draw.Draw(white, bounds, &image.Uniform{C: color.White}, image.Point{}, draw.Src)
draw.Draw(white, bounds, img, bounds.Min, draw.Over)
draw.Draw(white, bounds, oriented, bounds.Min, draw.Over)
w, h := fitDimensions(bounds.Dx(), bounds.Dy(), 256)
thumbnail := imaging.Resize(white, w, h, imaging.Lanczos)
crop := imaging.Fill(white, 128, 128, imaging.Center, imaging.Lanczos)