package image type Scale struct { Width uint Height uint Scale float64 } func NewScale(width uint, height uint, scale float64) *Scale { return &Scale{width, height, scale} } // MinRatio 获取缩放比例,等比缩放,以短边为准 func (s Scale) MinRatio(width, height uint) Scale { scale := 1.0 if s.Width/s.Height > width/height { if s.Height > height { scale = CustomScale(height, s.Height) } } else { if s.Width > width { scale = CustomScale(width, s.Width) } } width = uint(scale * float64(s.Width)) height = uint(scale * float64(s.Height)) return Scale{Width: width, Height: height, Scale: scale} } // MaxRatio 裁切,以长边为准 func (s Scale) MaxRatio(width, height uint) Scale { scale := 1.0 if s.Width/s.Height > width/height { scale = CustomScale(height, s.Height) } else { scale = CustomScale(width, s.Width) } width = uint(scale * float64(s.Width)) height = uint(scale * float64(s.Height)) return Scale{Width: width, Height: height, Scale: scale} }