-
-
Notifications
You must be signed in to change notification settings - Fork 461
/
image_test.go
180 lines (154 loc) · 4.36 KB
/
image_test.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
package main
import (
"io/ioutil"
"testing"
)
func TestImageResize(t *testing.T) {
t.Run("Width and Height defined", func(t *testing.T) {
opts := ImageOptions{Width: 300, Height: 300}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := Resize(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/jpeg" {
t.Error("Invalid image MIME type")
}
if assertSize(img.Body, opts.Width, opts.Height) != nil {
t.Errorf("Invalid image size, expected: %dx%d", opts.Width, opts.Height)
}
})
t.Run("Width defined", func(t *testing.T) {
opts := ImageOptions{Width: 300}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := Resize(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/jpeg" {
t.Error("Invalid image MIME type")
}
if err := assertSize(img.Body, 300, 404); err != nil {
t.Error(err)
}
})
t.Run("Width defined with NoCrop=false", func(t *testing.T) {
opts := ImageOptions{Width: 300, NoCrop: false, IsDefinedField: IsDefinedField{NoCrop: true}}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := Resize(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/jpeg" {
t.Error("Invalid image MIME type")
}
// The original image is 550x740
if err := assertSize(img.Body, 300, 740); err != nil {
t.Error(err)
}
})
t.Run("Width defined with NoCrop=true", func(t *testing.T) {
opts := ImageOptions{Width: 300, NoCrop: true, IsDefinedField: IsDefinedField{NoCrop: true}}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := Resize(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/jpeg" {
t.Error("Invalid image MIME type")
}
// The original image is 550x740
if err := assertSize(img.Body, 300, 404); err != nil {
t.Error(err)
}
})
}
func TestImageFit(t *testing.T) {
opts := ImageOptions{Width: 300, Height: 300}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := Fit(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/jpeg" {
t.Error("Invalid image MIME type")
}
// 550x740 -> 222.9x300
if assertSize(img.Body, 223, 300) != nil {
t.Errorf("Invalid image size, expected: %dx%d", opts.Width, opts.Height)
}
}
func TestImageAutoRotate(t *testing.T) {
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := AutoRotate(buf, ImageOptions{})
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/jpeg" {
t.Error("Invalid image MIME type")
}
if assertSize(img.Body, 550, 740) != nil {
t.Errorf("Invalid image size, expected: %dx%d", 550, 740)
}
}
func TestImagePipelineOperations(t *testing.T) {
width, height := 300, 260
operations := PipelineOperations{
PipelineOperation{
Name: "crop",
Params: map[string]interface{}{
"width": width,
"height": height,
},
},
PipelineOperation{
Name: "convert",
Params: map[string]interface{}{
"type": "webp",
},
},
}
opts := ImageOptions{Operations: operations}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))
img, err := Pipeline(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/webp" {
t.Error("Invalid image MIME type")
}
if assertSize(img.Body, width, height) != nil {
t.Errorf("Invalid image size, expected: %dx%d", width, height)
}
}
func TestCalculateDestinationFitDimension(t *testing.T) {
cases := []struct {
// Image
imageWidth int
imageHeight int
// User parameter
optionWidth int
optionHeight int
// Expect
fitWidth int
fitHeight int
}{
// Leading Width
{1280, 1000, 710, 9999, 710, 555},
{1279, 1000, 710, 9999, 710, 555},
{900, 500, 312, 312, 312, 173}, // rounding down
{900, 500, 313, 313, 313, 174}, // rounding up
// Leading height
{1299, 2000, 710, 999, 649, 999},
{1500, 2000, 710, 999, 710, 947},
}
for _, tc := range cases {
fitWidth, fitHeight := calculateDestinationFitDimension(tc.imageWidth, tc.imageHeight, tc.optionWidth, tc.optionHeight)
if fitWidth != tc.fitWidth || fitHeight != tc.fitHeight {
t.Errorf(
"Fit dimensions calculation failure\nExpected : %d/%d (width/height)\nActual : %d/%d (width/height)\n%+v",
tc.fitWidth, tc.fitHeight, fitWidth, fitHeight, tc,
)
}
}
}