/
image.go
191 lines (157 loc) · 4.65 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
package main
import (
"image"
_ "image/gif"
_ "image/jpeg"
"image/png"
"io"
"log"
"net"
"net/http"
"os"
"time"
"github.com/dchest/uniuri" // give us random URIs
"github.com/nfnt/resize"
"github.com/oliamb/cutter"
)
// Take the site image and save in our server
func GetImage(db DB, imageUrl string) (*Image, error) {
// My own Cient with my own Transport
// Just to abort very slow responses
transport := http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, time.Duration(10*time.Second))
},
}
client := http.Client{
Transport: &transport,
}
res, err := client.Get(imageUrl)
if err != nil {
return nil, err
}
defer res.Body.Close()
image, err := SaveImage(res.Body)
if err != nil {
return nil, err
}
return image, nil
}
type dimension struct {
Width int
Height int
}
func SaveImage(file io.Reader) (*Image, error) {
log.Println("Starting SaveImage")
imageId := uniuri.NewLen(20)
i, err := db.Get(Image{}, imageId)
for err == nil && i != nil {
// Shit, we generated an existing ImageId!
// Aren't we so lucky?!
imageId := uniuri.NewLen(20)
i, err = db.Get(Image{}, imageId)
}
if err != nil {
return nil, err
}
// Lets decode our image to work with it
originalImg, _, err := image.Decode(file)
if err != nil {
return nil, err
}
originalWidht := originalImg.Bounds().Dx()
originalHeight := originalImg.Bounds().Dy()
maxSize := ""
log.Printf("\nPreparing to save de image %dx%d\n", originalWidht, originalHeight)
// We will save 3 resized images
sizes := [...]string{"small", "medium", "large"}
dimensions := [...]dimension{{261, 142}, {358, 194}, {748, 408}}
// We will save the Thumbnails just if the original image
// dimension is bigger or equals than the thumbnail dimension
// to large thumbnail images
for index, size := range sizes {
newWidth := dimensions[index].Width
newHeight := dimensions[index].Height
widthRatio := float32(originalWidht) / float32(newWidth) // Width ratio
heightRatio := float32(originalHeight) / float32(newHeight) // Height ratio
log.Printf("Trying to save img size %d, widthRatio: %f, heightRatio: %f\n", size, widthRatio, heightRatio)
// We will resize and crop images small and medium and
// just images bigger than large size will be saved with large scale
if size == "small" || (widthRatio >= 1 && heightRatio >= 1) {
maxSize = size
// Below the values to the image be resized after be cropped
// If resized width or height == 0, so the proportion will be conserved
resizedWidth := 0 // Width to image be resized
resizedHeight := 0 // Height to image be resized
if widthRatio >= heightRatio {
// We will resize based on height and crop it after resized
resizedHeight = newHeight
} else {
// We will resize besed on height and crop it after resized
resizedWidth = newWidth
}
fo, err := os.Create("public/img/" + imageId + "-" + size + ".png")
if err != nil {
return nil, err
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
// If the image is bigger in both directions
if widthRatio >= 1 && heightRatio >= 1 {
// Downscales an image preserving its aspect ratio to the maximum dimensions
resizedImage := resize.Resize(uint(resizedWidth), uint(resizedHeight), originalImg, resize.Lanczos3)
// If image still bigger, lets crop it to the right size
croppedImg, err := cutter.Crop(resizedImage, cutter.Config{
Width: newWidth,
Height: newHeight,
Mode: cutter.Centered,
})
if err != nil {
return nil, err
}
png.Encode(fo, croppedImg)
} else {
// If the image isn't bigger in both directions
// We will enter here just if its the small/medium size image
if originalHeight < newHeight {
// If this image isn't taller enought
newHeight = originalHeight
}
if originalWidht < newWidth {
// If this image isn't large enought
newWidth = originalWidht
}
// If image still bigger, lets crop it to the right size
croppedImg, err := cutter.Crop(originalImg, cutter.Config{
Width: newWidth,
Height: newHeight,
Mode: cutter.Centered,
})
if err != nil {
return nil, err
}
png.Encode(fo, croppedImg)
}
log.Printf("Saving the image size %s\n", size)
}
}
if maxSize == "" {
// There is no image saved, cause the original image is so small
return nil, nil
}
image := &Image{
ImageId: imageId,
MaxSize: maxSize,
Creation: time.Now(),
Deleted: false,
}
err = db.Insert(image)
if err != nil {
return nil, err
}
return image, nil
}