-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize_constraints.go
More file actions
36 lines (29 loc) · 831 Bytes
/
resize_constraints.go
File metadata and controls
36 lines (29 loc) · 831 Bytes
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
package icat
import (
"image"
)
func resizeConstraints(imageBounds image.Rectangle, maxHeight, maxWidth int) (size int, option rune) {
imgHeight := imageBounds.Dy()
imgWidth := imageBounds.Dx()
if maxHeight == 0 || maxWidth == 0 {
return 0, '0' // Don't resize
}
if imgHeight <= maxHeight && imgWidth <= maxWidth {
return 0, '0' // Don't resize
}
if imgHeight <= maxHeight && imgWidth > maxWidth {
return maxWidth, 'x'
}
if imgWidth <= maxWidth && imgHeight > maxHeight {
return maxHeight, 'y'
}
// OK, both x and y are too big. Let's figure out which one is the biggest
// and use that to calculate the resize factor.
hRatio := float32(imgHeight) / float32(maxHeight)
wRatio := float32(imgWidth) / float32(maxWidth)
if hRatio > wRatio {
return maxHeight, 'y'
} else {
return maxWidth, 'x'
}
}