-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathimage.go
165 lines (140 loc) · 5.17 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
package gofakeit
import (
"bytes"
"errors"
img "image"
imgCol "image/color"
"image/jpeg"
"image/png"
"math/rand"
"strconv"
)
// ImageURL will generate a random Image Based Upon Height And Width. https://picsum.photos/
func ImageURL(width int, height int) string { return imageURL(globalFaker.Rand, width, height) }
// ImageURL will generate a random Image Based Upon Height And Width. https://picsum.photos/
func (f *Faker) ImageURL(width int, height int) string { return imageURL(f.Rand, width, height) }
func imageURL(r *rand.Rand, width int, height int) string {
return "https://picsum.photos/" + strconv.Itoa(width) + "/" + strconv.Itoa(height)
}
// Image generates a random rgba image
func Image(width int, height int) *img.RGBA { return image(globalFaker.Rand, width, height) }
// Image generates a random rgba image
func (f *Faker) Image(width int, height int) *img.RGBA { return image(f.Rand, width, height) }
func image(r *rand.Rand, width int, height int) *img.RGBA {
upLeft := img.Point{0, 0}
lowRight := img.Point{width, height}
img := img.NewRGBA(img.Rectangle{upLeft, lowRight})
// Set color for each pixel
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
img.Set(x, y, imgCol.RGBA{uint8(number(r, 0, 255)), uint8(number(r, 0, 255)), uint8(number(r, 0, 255)), 0xff})
}
}
return img
}
// ImageJpeg generates a random rgba jpeg image
func ImageJpeg(width int, height int) []byte { return imageJpeg(globalFaker.Rand, width, height) }
// ImageJpeg generates a random rgba jpeg image
func (f *Faker) ImageJpeg(width int, height int) []byte { return imageJpeg(f.Rand, width, height) }
func imageJpeg(r *rand.Rand, width int, height int) []byte {
buf := new(bytes.Buffer)
jpeg.Encode(buf, image(r, width, height), nil)
return buf.Bytes()
}
// ImagePng generates a random rgba png image
func ImagePng(width int, height int) []byte { return imagePng(globalFaker.Rand, width, height) }
// ImagePng generates a random rgba png image
func (f *Faker) ImagePng(width int, height int) []byte { return imagePng(f.Rand, width, height) }
func imagePng(r *rand.Rand, width int, height int) []byte {
buf := new(bytes.Buffer)
png.Encode(buf, image(r, width, height))
return buf.Bytes()
}
func addImageLookup() {
AddFuncLookup("imageurl", Info{
Display: "Image URL",
Category: "image",
Description: "Random image url",
Example: "https://picsum.photos/500/500",
Output: "string",
Params: []Param{
{Field: "width", Display: "Width", Type: "int", Default: "500", Description: "Image width in px"},
{Field: "height", Display: "Height", Type: "int", Default: "500", Description: "Image height in px"},
},
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
width, err := info.GetInt(m, "width")
if err != nil {
return nil, err
}
if width < 10 || width >= 1000 {
return nil, errors.New("invalid image width, must be greater than 10, less than 1000")
}
height, err := info.GetInt(m, "height")
if err != nil {
return nil, err
}
if height < 10 || height >= 1000 {
return nil, errors.New("invalid image height, must be greater than 10, less than 1000")
}
return imageURL(r, width, height), nil
},
})
AddFuncLookup("imagejpeg", Info{
Display: "Image JPEG",
Category: "image",
Description: "Random jpeg image",
Example: "file.jpeg - bytes",
Output: "[]byte",
ContentType: "image/jpeg",
Params: []Param{
{Field: "width", Display: "Width", Type: "int", Default: "500", Description: "Image width in px"},
{Field: "height", Display: "Height", Type: "int", Default: "500", Description: "Image height in px"},
},
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
width, err := info.GetInt(m, "width")
if err != nil {
return nil, err
}
if width < 10 || width >= 1000 {
return nil, errors.New("invalid image width, must be greater than 10, less than 1000")
}
height, err := info.GetInt(m, "height")
if err != nil {
return nil, err
}
if height < 10 || height >= 1000 {
return nil, errors.New("invalid image height, must be greater than 10, less than 1000")
}
return imageJpeg(r, width, height), nil
},
})
AddFuncLookup("imagepng", Info{
Display: "Image PNG",
Category: "image",
Description: "Random png image",
Example: "file.png - bytes",
Output: "[]byte",
ContentType: "image/png",
Params: []Param{
{Field: "width", Display: "Width", Type: "int", Default: "500", Description: "Image width in px"},
{Field: "height", Display: "Height", Type: "int", Default: "500", Description: "Image height in px"},
},
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
width, err := info.GetInt(m, "width")
if err != nil {
return nil, err
}
if width < 10 || width >= 1000 {
return nil, errors.New("invalid image width, must be greater than 10, less than 1000")
}
height, err := info.GetInt(m, "height")
if err != nil {
return nil, err
}
if height < 10 || height >= 1000 {
return nil, errors.New("invalid image height, must be greater than 10, less than 1000")
}
return imagePng(r, width, height), nil
},
})
}