This repository has been archived by the owner on Apr 22, 2019. It is now read-only.
/
ImageFile.go
136 lines (113 loc) · 2.94 KB
/
ImageFile.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
package outputs
import (
"errors"
"image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
"math"
"os"
"path"
"github.com/aerogo/ipo/inputs"
"github.com/chai2010/webp"
"github.com/nfnt/resize"
"github.com/aerogo/ipo"
)
// Force interface implementation
var _ ipo.Output = (*ImageFile)(nil)
// The threshold where we start seeing 2 aspect ratios as different
const aspectRatioThreshold = 0.02
// ImageFile writes an image to the filesystem.
type ImageFile struct {
Directory string
BaseName string
Format string
Width int
Height int
Quality int
}
// Write ...
func (file *ImageFile) Write(obj interface{}) error {
input := obj.(inputs.Image)
extension := input.Extension()
if extension == "" {
return errors.New("Unknown format: " + input.Format())
}
img := input.Image()
width := img.Bounds().Dx()
height := img.Bounds().Dy()
resizeXRequired := file.Width != 0 && file.Width < width
resizeYRequired := file.Height != 0 && file.Height < height
resizeRequired := resizeXRequired || resizeYRequired
// fmt.Println(file.BaseName+extension, "|", width, "x", height, "|", len(input.Data())/1024, "KB")
// Original file output
if file.Format == "" && !resizeRequired {
fullPath := path.Join(file.Directory, file.BaseName+extension)
return ioutil.WriteFile(fullPath, input.Data(), 0644)
}
// Resize if needed
if resizeRequired {
newWidth := uint(0)
newHeight := uint(0)
if file.Width != 0 && file.Height != 0 {
// Take aspect ratio of original image
aspectRatio := float64(width) / float64(height)
// Take required aspect ratio
requiredAspectRatio := float64(file.Width) / float64(file.Height)
// Decide what to do depending on the aspect ratio differences
if math.Abs(aspectRatio-requiredAspectRatio) <= aspectRatioThreshold {
newWidth = uint(file.Width)
newHeight = uint(file.Height)
} else if aspectRatio > requiredAspectRatio {
newHeight = uint(file.Height)
} else {
newWidth = uint(file.Width)
}
} else {
newWidth = uint(file.Width)
newHeight = uint(file.Height)
}
img = resize.Resize(newWidth, newHeight, img, resize.Lanczos3)
}
// Set format automatically if needed
if file.Format == "" {
file.Format = input.Format()
}
// Write data to file
fullPath := path.Join(file.Directory, file.BaseName+file.Extension())
stream, err := os.Create(fullPath)
if err != nil {
return err
}
defer stream.Close()
switch file.Format {
case "jpg", "jpeg":
err = jpeg.Encode(stream, img, &jpeg.Options{
Quality: file.Quality,
})
case "png":
err = png.Encode(stream, img)
case "gif":
err = gif.Encode(stream, img, nil)
case "webp":
err = webp.Encode(stream, img, &webp.Options{
Quality: float32(file.Quality),
})
}
return err
}
// Extension ...
func (file *ImageFile) Extension() string {
switch file.Format {
case "jpg", "jpeg":
return ".jpg"
case "png":
return ".png"
case "gif":
return ".gif"
case "webp":
return ".webp"
default:
return ""
}
}