forked from shenghui0779/yiigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
271 lines (239 loc) · 6.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package yiigo
import (
"errors"
"fmt"
"image"
"io"
"os"
"git.sr.ht/~sbinet/gg"
"github.com/disintegration/imaging"
"github.com/rwcarlsen/goexif/exif"
"github.com/shopspring/decimal"
)
const MediaThumbnailWidth = 200
// Orientation 图片的旋转方向
type Orientation int
func (o Orientation) String() string {
s := ""
switch o {
case TopLeft:
s = "Top-Left"
case TopRight:
s = "Top-Right"
case BottomRight:
s = "Bottom-Right"
case BottomLeft:
s = "Bottom-Left"
case LeftTop:
s = "Left-Top"
case RightTop:
s = "Right-Top"
case RightBottom:
s = "Right-Bottom"
case LeftBottom:
s = "Left-Bottom"
}
return s
}
const (
TopLeft Orientation = 1
TopRight Orientation = 2
BottomRight Orientation = 3
BottomLeft Orientation = 4
LeftTop Orientation = 5
RightTop Orientation = 6
RightBottom Orientation = 7
LeftBottom Orientation = 8
)
// ImageEXIF 定义图片EXIF
type ImageEXIF struct {
Size int64
Format string
Width int
Height int
Orientation string
Longitude decimal.Decimal
Latitude decimal.Decimal
}
// ParseImageEXIF 解析图片EXIF
func ParseImageEXIF(filename string) (*ImageEXIF, error) {
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("os.Open: %w", err)
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("os.File.Stat: %w", err)
}
data := &ImageEXIF{
Size: stat.Size(),
}
format, _ := imaging.FormatFromFilename(filename)
if format < 0 {
return data, nil
}
// 图片格式
data.Format = format.String()
if x, _ := exif.Decode(f); x != nil {
// 经纬度
lat, lng, _ := x.LatLong()
data.Longitude = decimal.NewFromFloat(lng)
data.Latitude = decimal.NewFromFloat(lat)
// 宽
if tag, _ := x.Get(exif.PixelXDimension); tag != nil {
v, _ := tag.Int(0)
data.Width = v
}
// 高
if tag, _ := x.Get(exif.PixelYDimension); tag != nil {
v, _ := tag.Int(0)
data.Height = v
}
// 转向
if tag, _ := x.Get(exif.Orientation); tag != nil {
v, _ := tag.Int(0)
data.Orientation = Orientation(v).String()
}
}
if data.Width == 0 || data.Height == 0 {
if img, _ := imaging.Open(filename); img != nil {
rect := img.Bounds()
data.Width = rect.Dx()
data.Height = rect.Dy()
}
}
return data, nil
}
// Rect 定义一个矩形框
type Rect struct {
X int
Y int
W int
H int
}
// ImageThumbnail 图片缩略图
func ImageThumbnail(w io.Writer, filename string, rect *Rect, options ...imaging.EncodeOption) error {
if rect == nil || rect.W < 0 || rect.H < 0 {
return errors.New("invalid param rect")
}
img, err := imaging.Open(filename)
if err != nil {
return err
}
size := img.Bounds().Size()
if rect.W == 0 && rect.H == 0 {
rect.W = MediaThumbnailWidth
rect.H = rect.W * size.Y / size.X
} else {
if rect.W > size.X {
rect.W = size.X
}
if rect.H > size.Y {
rect.H = size.Y
}
if rect.W > 0 {
if rect.H == 0 {
rect.H = rect.W * size.Y / size.X
}
} else {
rect.W = rect.H * size.X / size.Y
}
}
thumbnail := imaging.Thumbnail(img, rect.W, rect.H, imaging.Lanczos)
format, _ := imaging.FormatFromFilename(filename)
return imaging.Encode(w, thumbnail, format, options...)
}
// ImageThumbnailFromReader 图片缩略图
func ImageThumbnailFromReader(w io.Writer, r io.Reader, format imaging.Format, rect *Rect, options ...imaging.EncodeOption) error {
if rect == nil || rect.W < 0 || rect.H < 0 {
return errors.New("invalid param rect")
}
img, err := imaging.Decode(r)
if err != nil {
return err
}
size := img.Bounds().Size()
if rect.W == 0 && rect.H == 0 {
rect.W = MediaThumbnailWidth
rect.H = rect.W * size.Y / size.X
} else {
if rect.W > size.X {
rect.W = size.X
}
if rect.H > size.Y {
rect.H = size.Y
}
if rect.W > 0 {
if rect.H == 0 {
rect.H = rect.W * size.Y / size.X
}
} else {
rect.W = rect.H * size.X / size.Y
}
}
thumbnail := imaging.Thumbnail(img, rect.W, rect.H, imaging.Lanczos)
return imaging.Encode(w, thumbnail, format, options...)
}
// ImageCrop 图片裁切
func ImageCrop(w io.Writer, filename string, rect *Rect, options ...imaging.EncodeOption) error {
if rect == nil || rect.X < 0 || rect.Y < 0 || rect.W <= 0 || rect.H <= 0 {
return errors.New("invalid param rect")
}
img, err := imaging.Open(filename)
if err != nil {
return err
}
crop := imaging.Crop(img, image.Rect(rect.X, rect.Y, rect.X+rect.W, rect.Y+rect.H))
format, _ := imaging.FormatFromFilename(filename)
return imaging.Encode(w, crop, format, options...)
}
// ImageCropFromReader 图片裁切
func ImageCropFromReader(w io.Writer, r io.Reader, format imaging.Format, rect *Rect, options ...imaging.EncodeOption) error {
if rect == nil || rect.X < 0 || rect.Y < 0 || rect.W < 0 || rect.H < 0 {
return errors.New("invalid param rect")
}
img, err := imaging.Decode(r)
if err != nil {
return err
}
crop := imaging.Crop(img, image.Rect(rect.X, rect.Y, rect.X+rect.W, rect.Y+rect.H))
return imaging.Encode(w, crop, format, options...)
}
// ImageLabel 图片标注
func ImageLabel(w io.Writer, filename string, rects []*Rect, options ...imaging.EncodeOption) error {
img, err := imaging.Open(filename)
if err != nil {
return err
}
dc := gg.NewContextForImage(img)
dc.SetRGB255(255, 0, 0)
dc.SetLineWidth(8)
for _, rect := range rects {
if rect.X < 0 || rect.Y < 0 || rect.W <= 0 || rect.H <= 0 {
return errors.New("invalid param rects")
}
dc.DrawRectangle(float64(rect.X), float64(rect.Y), float64(rect.W), float64(rect.H))
}
dc.Stroke()
format, _ := imaging.FormatFromFilename(filename)
return imaging.Encode(w, dc.Image(), format, options...)
}
// ImageLabelFromReader 图片标注
func ImageLabelFromReader(w io.Writer, r io.Reader, format imaging.Format, rects []*Rect, options ...imaging.EncodeOption) error {
img, err := imaging.Decode(r)
if err != nil {
return err
}
dc := gg.NewContextForImage(img)
dc.SetRGB255(255, 0, 0)
dc.SetLineWidth(8)
for _, rect := range rects {
if rect.X < 0 || rect.Y < 0 || rect.W <= 0 || rect.H <= 0 {
return errors.New("invalid param rects")
}
dc.DrawRectangle(float64(rect.X), float64(rect.Y), float64(rect.W), float64(rect.H))
}
dc.Stroke()
return imaging.Encode(w, dc.Image(), format, options...)
}