This repository was archived by the owner on Apr 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfactory.go
More file actions
177 lines (156 loc) · 4.34 KB
/
factory.go
File metadata and controls
177 lines (156 loc) · 4.34 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package imgfactory
import (
"image"
"image/color"
"image/draw"
"github.com/FloatTech/gg"
"github.com/disintegration/imaging"
)
// Factory 处理中图像
type Factory struct {
im *image.NRGBA
}
func NewFactory(im *image.NRGBA) *Factory {
return &Factory{im: im}
}
func (dst *Factory) W() int {
return dst.im.Bounds().Size().X
}
func (dst *Factory) H() int {
return dst.im.Bounds().Size().Y
}
func (dst *Factory) Image() *image.NRGBA {
return dst.im
}
// Clone 克隆
func (dst *Factory) Clone() *Factory {
var src Factory
sz := dst.im.Bounds().Size()
src.im = image.NewNRGBA(image.Rect(0, 0, sz.X, sz.Y))
draw.Over.Draw(src.im, src.im.Bounds(), dst.im, dst.im.Bounds().Min)
return &src
}
// Reshape 变形
func (dst *Factory) Reshape(w, h int) *Factory {
dst = Size(dst.im, w, h)
return dst
}
// FlipH 水平翻转
func (dst *Factory) FlipH() *Factory {
return &Factory{
im: imaging.FlipH(dst.im),
}
}
// FlipV 垂直翻转
func (dst *Factory) FlipV() *Factory {
return &Factory{
im: imaging.FlipV(dst.im),
}
}
// InsertUp 上部插入图片
func (dst *Factory) InsertUp(im image.Image, w, h, x, y int) *Factory {
im1 := Size(im, w, h).im
// 叠加图片
draw.Over.Draw(dst.im, dst.im.Bounds(), im1, im1.Bounds().Min.Sub(image.Pt(x, y)))
return dst
}
// InsertUpC 上部插入图片 x,y是中心点
func (dst *Factory) InsertUpC(im image.Image, w, h, x, y int) *Factory {
im1 := Size(im, w, h)
// 叠加图片
draw.Over.Draw(dst.im, dst.im.Bounds(), im1.im, im1.im.Bounds().Min.Sub(image.Pt(x-im1.im.Bounds().Max.X/2, y-im1.im.Bounds().Max.Y/2)))
return dst
}
// InsertBottom 底部插入图片
func (dst *Factory) InsertBottom(im image.Image, w, h, x, y int) *Factory {
im1 := Size(im, w, h).im
dc := dst.Clone()
sz := dst.im.Bounds().Size()
dst = NewFactoryBG(sz.X, sz.Y, color.NRGBA{0, 0, 0, 0})
draw.Over.Draw(dst.im, dst.im.Bounds(), im1, im1.Bounds().Min.Sub(image.Pt(x, y)))
draw.Over.Draw(dst.im, dst.im.Bounds(), dc.im, dc.im.Bounds().Min)
return dst
}
// InsertBottomC 底部插入图片 x,y是中心点
func (dst *Factory) InsertBottomC(im image.Image, w, h, x, y int) *Factory {
im1 := Size(im, w, h)
dc := dst.Clone()
sz := dst.im.Bounds().Size()
dst = NewFactoryBG(sz.X, sz.Y, color.NRGBA{0, 0, 0, 0})
draw.Over.Draw(dst.im, dst.im.Bounds(), im1.im, im1.im.Bounds().Min.Sub(image.Pt(x-im1.im.Bounds().Max.X/2, y-im1.im.Bounds().Max.Y/2)))
draw.Over.Draw(dst.im, dst.im.Bounds(), dc.im, dc.im.Bounds().Min)
return dst
}
// Circle 获取圆图
func (dst *Factory) Circle(r int) *Factory {
sz := dst.im.Bounds().Size()
if r == 0 {
r = sz.Y / 2
}
dst = dst.Reshape(2*r, 2*r)
b := dst.im.Bounds()
for y1 := b.Min.Y; y1 < b.Max.Y; y1++ {
for x1 := b.Min.X; x1 < b.Max.X; x1++ {
if (x1-r)*(x1-r)+(y1-r)*(y1-r) > r*r {
dst.im.Set(x1, y1, color.NRGBA{0, 0, 0, 0})
}
}
}
return dst
}
// Clip 剪取方图
func (dst *Factory) Clip(w, h, x, y int) *Factory {
dst.im = dst.im.SubImage(image.Rect(x, y, x+w, y+h)).(*image.NRGBA)
return dst
}
// ClipCircleFix 裁取圆图
func (dst *Factory) ClipCircleFix(x, y, r int) *Factory {
dst = dst.Clip(2*r, 2*r, x-r, y-r)
b := dst.im.Bounds()
for y1 := b.Min.Y; y1 < b.Max.Y; y1++ {
for x1 := b.Min.X; x1 < b.Max.X; x1++ {
if (x1-x)*(x1-x)+(y1-y)*(y1-y) > r*r {
dst.im.Set(x1, y1, color.NRGBA{0, 0, 0, 0})
}
}
}
return dst
}
// ClipCircle 扣取圆
func (dst *Factory) ClipCircle(x, y, r int) *Factory {
// dc := dst.Clip(x-r, y-r, 2*r, 2*r)
b := dst.im.Bounds()
for y1 := b.Min.Y; y1 < b.Max.Y; y1++ {
for x1 := b.Min.X; x1 < b.Max.X; x1++ {
if (x1-x)*(x1-x)+(y1-y)*(y1-y) <= r*r {
dst.im.Set(x1, y1, color.NRGBA{0, 0, 0, 0})
}
}
}
return dst
}
// InsertText 插入文本
func (dst *Factory) InsertText(font string, size float64, col []int, x, y float64, txt string) *Factory {
dc := gg.NewContextForImage(dst.im)
// 字体, 大小, 颜色, 位置
err := dc.LoadFontFace(font, size)
if err != nil {
return dst
}
dc.SetRGBA255(col[0], col[1], col[2], col[3])
dc.DrawString(txt, x, y)
ds := dc.Image()
draw.Over.Draw(dst.im, dst.im.Bounds(), ds, ds.Bounds().Min)
return dst
}
// InsertUpG gif 上部插入图片
func (dst *Factory) InsertUpG(im []*image.NRGBA, w, h, x, y int) []*image.NRGBA {
if len(im) == 0 {
return nil
}
ims := make([]*image.NRGBA, len(im))
for i, v := range im {
ims[i] = dst.Clone().InsertUp(v, w, h, x, y).im
}
return ims
}