-
Notifications
You must be signed in to change notification settings - Fork 8
/
crop.go
102 lines (95 loc) · 2.6 KB
/
crop.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package imageutil
import (
"image"
"image/color"
"image/draw"
"strings"
)
const (
AlignTop = "top"
AlignCenter = "center"
AlignBottom = "bottom"
AlignLeft = "left"
AlignRight = "right"
)
// Crop takes an image and crops it to the specified rectangle.
func Crop(src image.Image, retain image.Rectangle) image.Image {
new := image.NewRGBA(retain)
draw.Draw(new, new.Bounds(), src, retain.Min, draw.Over)
return new
}
// CropX crops an image by its width horizontally.
func CropX(src image.Image, width uint, align string) image.Image {
if int(width) >= src.Bounds().Dx() {
return src
}
var xMin int
switch strings.ToLower(strings.TrimSpace(align)) {
case AlignLeft:
xMin = src.Bounds().Min.X
case AlignRight:
xMin = src.Bounds().Max.X - int(width)
default:
xMin = (src.Bounds().Max.X - int(width)) / 2
}
return Crop(src, image.Rect(
xMin,
src.Bounds().Min.Y,
xMin+int(width),
src.Bounds().Max.Y))
}
// CropY crops an image by its height vertically.
func CropY(src image.Image, height uint, align string) image.Image {
if int(height) >= src.Bounds().Dy() {
return src
}
var yMin int
switch strings.ToLower(strings.TrimSpace(align)) {
case AlignTop:
yMin = src.Bounds().Min.Y
case AlignBottom:
yMin = src.Bounds().Max.Y - int(height)
default:
yMin = (src.Bounds().Max.Y - int(height)) / 2
}
return Crop(src, image.Rect(
src.Bounds().Min.X,
yMin,
src.Bounds().Max.X,
yMin+int(height)))
}
// SquareLarger returns an image that is cropped to where the height and weight are equal
// to the larger of the source image.
func SquareLarger(src image.Image, bgcolor color.Color) image.Image {
width := src.Bounds().Dx()
height := src.Bounds().Dy()
switch {
case width > height:
new := AddBackgroundColor(image.NewRGBA(image.Rect(0, 0, width, width)), bgcolor)
draw.Draw(new, new.Bounds(), src, image.Point{
Y: src.Bounds().Min.Y + ((height - width) / 2),
X: src.Bounds().Min.X}, draw.Over)
return new
case width < height:
new := AddBackgroundColor(image.NewRGBA(image.Rect(0, 0, height, height)), bgcolor)
draw.Draw(new, new.Bounds(), src, image.Point{
X: src.Bounds().Min.X + ((width - height) / 2),
Y: src.Bounds().Min.Y}, draw.Over)
return new
default:
return src
}
}
// Square returns an image that is cropped to where the height and weight are equal to the smaller of the source image.
func Square(src image.Image) image.Image {
width := src.Bounds().Dx()
height := src.Bounds().Dy()
switch {
case width > height:
return CropX(src, uint(height), AlignCenter)
case width < height:
return CropY(src, uint(width), AlignCenter)
default:
return src
}
}