webp fix v2

This commit is contained in:
tamakyi
2026-10-16 09:39:30 +08:00
parent e4457e3da4
commit da7e76a7db
10 changed files with 903 additions and 60 deletions
+48 -1
View File
@@ -208,7 +208,7 @@ func TestConvertStaticImageToWebPAppliesEXIFOrientation(t *testing.T) {
t.Fatalf("raw jpeg dims = %dx%d, want 40x30", cfg.Width, cfg.Height)
}
out, err := ConvertStaticImageToWebP(bytes.NewReader(data), EncodeSpec{MaxSide: 1600, Quality: 80})
out, err := ConvertStaticImageToWebP(data, EncodeSpec{MaxSide: 1600, Quality: 80})
if err != nil {
t.Fatalf("ConvertStaticImageToWebP: %v", err)
}
@@ -220,3 +220,50 @@ func TestConvertStaticImageToWebPAppliesEXIFOrientation(t *testing.T) {
t.Fatalf("webp dims = %dx%d, want 30x40 (orientation 6 applied)", w, h)
}
}
// 回归:比例校验必须与转码时的 EXIF 归一用同一套方向。
//
// 手机竖拍照片存成 40x30 像素但 EXIF 声明 Orientation=6,肉眼看到的是 30x40 的竖图。
// 若比例校验只看原始像素宽高(30/40 的横躺比例),一张合规的方形头像会被判为
// 「比例不符」而拒——这正是线上验证曾发现的缺口。
func TestAspectCheckAppliesEXIFOrientation(t *testing.T) {
// 像素 40x30 + Orientation=6 → 归一后 30x40(竖图 3:4,不是 1:1)
if err := (&aspect{W: 1, H: 1}).check(image.Config{Width: 40, Height: 30}, exifOrientRightTop); err == nil {
t.Fatal("40x30 + ori6 归一后是 3:4 竖图,头像应判为比例不符")
}
// 像素 40x30 + Orientation=1 → 仍是 3:4 竖图,同样应拒
if err := (&aspect{W: 1, H: 1}).check(image.Config{Width: 40, Height: 30}, exifOrientTopLeft); err == nil {
t.Fatal("40x30 ori1 是 3:4 竖图,头像应判为比例不符")
}
// 像素 40x40 + Orientation=6 → 归一后仍 40x40,头像应放行
if err := (&aspect{W: 1, H: 1}).check(image.Config{Width: 40, Height: 40}, exifOrientRightTop); err != nil {
t.Fatalf("40x40 + ori6 应放行: %v", err)
}
// 背景 16:9:像素 160x90 + Orientation=6 → 归一后 90x160(9:16)应拒
if err := (&aspect{W: 16, H: 9}).check(image.Config{Width: 160, Height: 90}, exifOrientRightTop); err == nil {
t.Fatal("160x90 + ori6 归一后是 9:16 竖图,背景应判为比例不符")
}
// nil aspect 表示该用途不校验比例(如通用图)
if err := (*aspect)(nil).check(image.Config{Width: 4000, Height: 3}, exifOrientRightTop); err != nil {
t.Fatalf("nil aspect 不应校验: %v", err)
}
}
// 回归:EXIF 归一会交换宽高的四种方向,与不交换的三种。
func TestOrientationSwapsDimensions(t *testing.T) {
swaps := map[int]bool{
exifOrientTopLeft: false, exifOrientTopRight: false,
exifOrientBottomRight: false, exifOrientBottomLeft: false,
exifOrientLeftTop: true, exifOrientRightTop: true,
exifOrientRightBottom: true, exifOrientLeftBottom: true,
}
for ori, want := range swaps {
if got := orientationSwapsDimensions(ori); got != want {
t.Errorf("orientationSwapsDimensions(%d) = %v, want %v", ori, got, want)
}
}
}