/
image.go
45 lines (34 loc) · 1.17 KB
/
image.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
42
43
44
45
package paint
import (
"github.com/nfnt/resize"
"image"
)
func ResizeToInch(source image.Image, maxWidth float64, maxHeight float64, dpi int) image.Image {
maxWidthPx := uint(InchToPixel(dpi, maxWidth))
maxHeightPx := uint(InchToPixel(dpi, maxHeight))
return ResizeWithRatio(source, maxWidthPx, maxHeightPx)
}
func CentimeterToPixel(dpi int, centi float64) int {
return InchToPixel(dpi, CentimeterToInch(centi))
}
func InchToPixel(dpi int, maxWidth float64) int {
return int(float64(dpi) * maxWidth)
}
func CentimeterToInch(centi float64) float64 {
return centi * 0.393701
}
func Resize(source image.Image, width uint, height uint) image.Image {
return resize.Resize(width, height, source, resize.Bilinear)
}
func ResizeA(source image.Image, width uint, height uint, a resize.InterpolationFunction) image.Image {
return resize.Resize(width, height, source, a)
}
func ResizeWithRatio(source image.Image, maxWidthPx uint, maxHeightPx uint) image.Image {
width := source.Bounds().Dx()
height := source.Bounds().Dy()
if width > height {
return resize.Resize(maxWidthPx, 0, source, resize.Lanczos3)
} else {
return resize.Resize(0, maxHeightPx, source, resize.Lanczos3)
}
}