-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage.go
More file actions
142 lines (125 loc) · 4 KB
/
image.go
File metadata and controls
142 lines (125 loc) · 4 KB
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
package gofaker
import (
"bytes"
"errors"
"image"
"image/color"
"image/jpeg"
"image/png"
"strconv"
)
// ImageURL will generate a random Image Based Upon Height And Width. https://picsum.photos/
func ImageURL(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) *image.RGBA {
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
// Set color for each pixel
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
img.Set(x, y, color.RGBA{uint8(Number(0, 255)), uint8(Number(0, 255)), uint8(Number(0, 255)), 0xff})
}
}
return img
}
// ImageJpeg generates a random rgba jpeg image
func ImageJpeg(width int, height int) []byte {
buf := new(bytes.Buffer)
jpeg.Encode(buf, Image(width, height), nil)
return buf.Bytes()
}
// ImagePng generates a random rgba png image
func ImagePng(width int, height int) []byte {
buf := new(bytes.Buffer)
png.Encode(buf, Image(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"},
},
Call: func(m *map[string][]string, 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(width, height), nil
},
})
AddFuncLookup("imagejpeg", Info{
Display: "Image JPEG",
Category: "image",
Description: "Random jpeg image",
Example: "file.jpeg - bytes",
Output: "[]byte",
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"},
},
Call: func(m *map[string][]string, 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(width, height), nil
},
})
AddFuncLookup("imagepng", Info{
Display: "Image PNG",
Category: "image",
Description: "Random png image",
Example: "file.png - bytes",
Output: "[]byte",
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"},
},
Call: func(m *map[string][]string, 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(width, height), nil
},
})
}