forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
122 lines (103 loc) · 3.7 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
package gofakeit
import (
"bytes"
"errors"
img "image"
imgCol "image/color"
"image/jpeg"
"image/png"
)
// Image generates a random rgba image
func Image(width int, height int) *img.RGBA { return image(GlobalFaker, width, height) }
// Image generates a random rgba image
func (f *Faker) Image(width int, height int) *img.RGBA { return image(f, width, height) }
func image(f *Faker, 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(f, 0, 255)), uint8(number(f, 0, 255)), uint8(number(f, 0, 255)), 0xff})
}
}
return img
}
// ImageJpeg generates a random rgba jpeg image
func ImageJpeg(width int, height int) []byte { return imageJpeg(GlobalFaker, width, height) }
// ImageJpeg generates a random rgba jpeg image
func (f *Faker) ImageJpeg(width int, height int) []byte { return imageJpeg(f, width, height) }
func imageJpeg(f *Faker, width int, height int) []byte {
buf := new(bytes.Buffer)
jpeg.Encode(buf, image(f, width, height), nil)
return buf.Bytes()
}
// ImagePng generates a random rgba png image
func ImagePng(width int, height int) []byte { return imagePng(GlobalFaker, width, height) }
// ImagePng generates a random rgba png image
func (f *Faker) ImagePng(width int, height int) []byte { return imagePng(f, width, height) }
func imagePng(f *Faker, width int, height int) []byte {
buf := new(bytes.Buffer)
png.Encode(buf, image(f, width, height))
return buf.Bytes()
}
func addImageLookup() {
AddFuncLookup("imagejpeg", Info{
Display: "Image JPEG",
Category: "image",
Description: "Image file format known for its efficient compression and compatibility",
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(f *Faker, m *MapParams, info *Info) (any, 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(f, width, height), nil
},
})
AddFuncLookup("imagepng", Info{
Display: "Image PNG",
Category: "image",
Description: "Image file format known for its lossless compression and support for transparency",
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(f *Faker, m *MapParams, info *Info) (any, 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(f, width, height), nil
},
})
}