webp fix v2
This commit is contained in:
@@ -63,17 +63,24 @@ const DefaultAspectTolerance = 0.01
|
||||
// 与 CheckPixelLimit 一致:只做 DecodeConfig 级轻量检查,不解码全图。
|
||||
// 注意:该方法会读取(消耗)传入的 reader,调用方需自行重建/复位 reader。
|
||||
func CheckAspectRatio(r io.Reader, wantW, wantH float64, tolerance float64) error {
|
||||
if wantW <= 0 || wantH <= 0 {
|
||||
return nil
|
||||
}
|
||||
cfg, _, err := image.DecodeConfig(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cfg.Width <= 0 || cfg.Height <= 0 {
|
||||
return CheckAspectRatioSize(cfg.Width, cfg.Height, wantW, wantH, tolerance)
|
||||
}
|
||||
|
||||
// CheckAspectRatioSize 是 CheckAspectRatio 的「宽高已知」版本。
|
||||
// 调用方若已从文件头拿到尺寸(或需要先按 EXIF Orientation 修正宽高后再比对),
|
||||
// 用本函数可避免重复读取文件头。
|
||||
func CheckAspectRatioSize(w, h int, wantW, wantH float64, tolerance float64) error {
|
||||
if wantW <= 0 || wantH <= 0 {
|
||||
return nil
|
||||
}
|
||||
actual := float64(cfg.Width) / float64(cfg.Height)
|
||||
if w <= 0 || h <= 0 {
|
||||
return nil
|
||||
}
|
||||
actual := float64(w) / float64(h)
|
||||
if math.Abs(actual-wantW/wantH) > tolerance {
|
||||
return ErrBadAspectRatio
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user