-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.go
162 lines (126 loc) · 3.48 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
package yuri
import (
"image"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"os"
"path"
"path/filepath"
"github.com/disintegration/imaging"
)
func DownloadFile(URL, dir, prefix string) (string, error) {
//Get the response bytes from the url
response, err := http.Get(URL)
if err != nil {
return "", err
}
defer response.Body.Close()
if !(response.StatusCode >= 200 && response.StatusCode <= 299) {
b, _ := ioutil.ReadAll(response.Body)
return "", &RequestError{Url: URL, Message: string(b), StatusCode: response.StatusCode}
}
file, err := ioutil.TempFile(dir, prefix)
if err != nil {
return "", err
////log.Fatal(err)
}
defer file.Close()
//Write the bytes to the fiel
_, err = io.Copy(file, response.Body)
if err != nil {
return "", err
}
return file.Name(), nil
}
func GetFileContentTypeWithExtension(paths string) (string, error) {
fileType := mime.TypeByExtension(path.Ext(paths))
if fileType == "" {
f, err := os.Open(paths)
if err != nil {
return "", err
}
defer f.Close()
fileType, err = GetFileContentType(f)
}
return fileType, nil
}
func GetFileContentType(out *os.File) (string, error) {
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err := out.Read(buffer)
if err != nil {
return "", err
}
// Use the net/http package's handy DectectContentType function. Always returns a valid
// content-type by returning "application/octet-stream" if no others seemed to match.
contentType := http.DetectContentType(buffer)
return contentType, nil
}
///do all function involving images like resizing them
// GetMultiPartFileContentType returns the content type of a file e.g
///image/png
func GetMultiPartFileContentType(file multipart.File) (string, error) {
// Only the first 512 bytes are used to sniff the content type.
fileHeader := make([]byte, 512)
if _, err := file.Read(fileHeader); err != nil {
return "", err
}
if _, err := file.Seek(0, 0); err != nil {
return "", err
}
// Use the net/http package's handy DectectContentType function. Always returns a valid
// content-type by returning "application/octet-stream" if no others seemed to match.
contentType := http.DetectContentType(fileHeader)
return contentType, nil
}
func GetFileExtension(name string) string {
extension := filepath.Ext(name)
return extension
}
func CreateATempFile(name string, file io.Reader) (*os.File, error) {
tempFile, err := ioutil.TempFile(os.TempDir(), name)
if err != nil {
return nil, err
}
_, err = io.Copy(tempFile, file)
return tempFile, err
}
// ResizeImage pass width to either height or width to maintain aspect ratio
func ResizeImage(images string, width, height int, dir, prefix string, extension string) (string, error) {
reader, err := os.Open(images)
if err != nil {
return "", err
}
withExtension, err := GetFileContentTypeWithExtension(extension)
if err != nil {
return "", err
}
var dst image.Image
defer reader.Close()
src, _, err := image.Decode(reader)
if err != nil {
return "", err
}
//log.Println("name is ",name)
dst = imaging.Resize(src, width, height, imaging.Lanczos)
file, err := ioutil.TempFile(dir, prefix)
if err != nil {
return "", err
}
defer file.Close()
format := FormatFromExtension(withExtension)
err = imaging.Encode(file, dst, format)
if err != nil {
return "", err
}
return file.Name(), nil
}
func FormatFromExtension(typeImg string) imaging.Format {
if typeImg == "image/png" {
return imaging.PNG
}
return imaging.JPEG
}