-
Notifications
You must be signed in to change notification settings - Fork 27
/
resize_scaledown.go
41 lines (32 loc) · 974 Bytes
/
resize_scaledown.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package vipsutil
import (
"sort"
)
// ResizingModeScaleDown shrink the image so that
// the resulting image is fully fit within ResizeDimensions.
type ResizingModeScaleDown struct{}
var _ ResizingMode = ResizingModeScaleDown{}
func (ResizingModeScaleDown) Resize(image ImageDimensions, resize ResizeDimensions) ResizeResult {
// If the source image is fit within ResizeDimensions, it is an noop.
if image.Width <= resize.Width && image.Height <= resize.Height {
return NoopResizeResult
}
var scales []float64
scaleX := div(resize.Width, image.Width)
scaledHeight := mul(image.Height, scaleX)
if scaledHeight <= resize.Height {
scales = append(scales, scaleX)
}
scaleY := div(resize.Height, image.Height)
scaledWidth := mul(image.Width, scaleY)
if scaledWidth <= resize.Width {
scales = append(scales, scaleY)
}
sort.Float64s(scales)
if len(scales) > 0 {
return ResizeResult{
Scale: scales[len(scales)-1],
}
}
return NoopResizeResult
}