-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
219 lines (204 loc) · 3.93 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
package zzblog
import (
"bytes"
"crypto/md5"
"encoding/hex"
"errors"
"github.com/nfnt/resize"
"golang.org/x/image/bmp"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"mime"
"os"
)
type ImageReader interface {
io.Reader
io.Seeker
}
const (
ImagePng = iota
ImageJpg
ImageGif
ImageBmp
ImageUnknown
)
type Image struct {
Id string
Width, Height uint
Format int
Pathfile string
}
func (img *Image) Resize(width, height uint) (bs []byte, err error) {
var bitmap image.Image
f, e := os.Open(img.Pathfile)
if e != nil {
err = e
return
}
defer f.Close()
var buf bytes.Buffer
if img.Format == ImageGif {
io.Copy(&buf, f)
bs = buf.Bytes()
return
}
if bitmap, err = decodeImage(img.Format, f); err != nil {
return
}
width, height = size(uint(img.Width), uint(img.Height), width, height, img.Format)
resized := resize.Resize(width, height, bitmap, resize.NearestNeighbor)
if err = encodeImage(&buf, img.Format, resized); err == nil {
bs = buf.Bytes()
}
return
}
func (img *Image) MimeType() string {
return mime.TypeByExtension(getImageExt(img.Format))
}
func size(ow, oh, w, h uint, format int) (width, height uint) {
if format == ImageGif {
width = ow
height = oh
return
}
if w > ow {
w = ow
}
if h > oh {
h = oh
}
if w != 0 {
if mh := w * oh / ow; h > mh {
h = mh
}
}
if h != 0 {
if mw := h * ow / oh; w > mw {
w = mw
}
}
width = w
height = h
return
}
func decodeImage(format int, reader ImageReader) (img image.Image, err error) {
reader.Seek(0, io.SeekStart)
switch format {
case ImageJpg:
img, err = jpeg.Decode(reader)
case ImagePng:
img, err = png.Decode(reader)
case ImageBmp:
img, err = bmp.Decode(reader)
case ImageGif:
img, err = gif.Decode(reader)
default:
err = errors.New("unknown format")
}
return
}
func encodeImage(writer io.Writer, format int, img image.Image) error {
switch format {
case ImagePng:
png.Encode(writer, img)
case ImageGif:
gif.Encode(writer, img, nil)
case ImageJpg:
jpeg.Encode(writer, img, nil)
case ImageBmp:
bmp.Encode(writer, img)
default:
return errors.New("unknown format")
}
return nil
}
func getImageExt(format int) string {
switch format {
case ImagePng:
return ".png"
case ImageJpg:
return ".jpg"
case ImageGif:
return ".gif"
case ImageBmp:
return ".bmp"
default:
return ""
}
}
func getImageId(r ImageReader) string {
buf := new(bytes.Buffer)
buf.ReadFrom(r)
md5Sumb := md5.Sum(buf.Bytes())
return hex.EncodeToString(md5Sumb[:])
}
func getImageFormat(r ImageReader) int {
bytes := make([]byte, 4)
r.Seek(0, io.SeekStart)
n, _ := r.Read(bytes)
if n < 4 {
return ImagePng
}
if bytes[0] == 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4E && bytes[3] == 0x47 {
return ImagePng
}
if bytes[0] == 0xFF && bytes[1] == 0xD8 {
return ImageJpg
}
if bytes[0] == 0x47 && bytes[1] == 0x49 && bytes[2] == 0x46 && bytes[3] == 0x38 {
return ImageGif
}
if bytes[0] == 0x42 && bytes[1] == 0x4D {
return ImageBmp
}
return ImageUnknown
}
func getImageConfig(format int, r ImageReader) (conf image.Config, err error) {
r.Seek(0, io.SeekStart)
switch format {
case ImageJpg:
conf, err = jpeg.DecodeConfig(r)
case ImagePng:
conf, err = png.DecodeConfig(r)
case ImageBmp:
conf, err = bmp.DecodeConfig(r)
case ImageGif:
conf, err = gif.DecodeConfig(r)
default:
err = errors.New("unknown format")
}
return
}
func getImageFromFile(f ImageReader) (img *Image, err error) {
img = new(Image)
img.Id = getImageId(f)
img.Format = getImageFormat(f)
if img.Format == ImageUnknown {
err = errors.New("unknown format")
return
}
conf, e := getImageConfig(img.Format, f)
if e != nil {
err = e
return
}
img.Height = uint(conf.Height)
img.Width = uint(conf.Width)
return
}
func getImage(file string) (img *Image, err error) {
f, e := os.Open(file)
if e != nil {
err = e
return
}
defer f.Close()
img, err = getImageFromFile(f)
if err == nil {
img.Pathfile = file
}
return
}