/
images.go
93 lines (71 loc) · 2 KB
/
images.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
package faker
import (
"fmt"
"strings"
"github.com/dimiro1/faker/defaults"
)
type ImageOptions struct {
Size string `default:"300x300"`
Format string `default:".jpg"`
}
type AvatarOptions struct {
Size string `default:"300"`
Format string `default:".jpg"`
Email string
}
// AvatarImage returns a Avatar image
func (f Faker) AvatarImage() string {
width := randomIntBetween(100, 400)
return f.AvatarImageWithOptions(AvatarOptions{
Size: fmt.Sprintf("%d", width),
Format: ".jpg",
Email: f.Email(),
})
}
// AvatarImageWithOptions returns a AvatarImage image
func (f Faker) AvatarImageWithOptions(o AvatarOptions) string {
defaults.Apply(&o)
if o.Email == "" {
panic(fmt.Sprintf("images: %s is empty", o.Email))
}
formats := []string{".jpg", ".png"}
valid := false
for _, format := range formats {
if o.Format == format {
valid = true
}
}
if !valid {
panic(fmt.Sprintf("images: %s is an invalid format, valid formats are %s", o.Format, formats))
}
return fmt.Sprintf("http://api.adorable.io/avatars/%s/%s%s", o.Size, o.Email, o.Format)
}
// PlaceholderImage returns a Placeholder image
func (f Faker) PlaceholderImage() string {
width := randomIntBetween(100, 400)
height := randomIntBetween(100, 400)
return f.PlaceholderImageWithOptions(ImageOptions{
Size: fmt.Sprintf("%dx%d", width, height),
})
}
// PlaceholderImageWithOptions returns a Placeholder image
func (f Faker) PlaceholderImageWithOptions(o ImageOptions) string {
defaults.Apply(&o)
formats := []string{".jpeg", ".jpg", ".gif", ".png"}
valid := false
for _, format := range formats {
if o.Format == format {
valid = true
}
}
if !valid {
panic(fmt.Sprintf("images: %s is an invalid format, valid formats are %s", o.Format, formats))
}
size := strings.Split(o.Size, "x")
if len(size) != 2 {
panic(fmt.Sprintf("images: %s is an invalid size, valid format is 300x300", o.Size))
}
width := size[0]
height := size[1]
return fmt.Sprintf("http://placehold.it/%sx%s%s", width, height, o.Format)
}