Skip to content

Commit af175b2

Browse files
committed
随机图片
1 parent 667657b commit af175b2

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

internet.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,31 @@ func (f *Faker) Email() (mail string) {
2929
"EmailDomain": f.RandomStringElement(p.Internet.FreeEmailDomains),
3030
})
3131
}
32+
33+
func (f *Faker) Image(width, height uint32) string {
34+
const (
35+
maxWidth = 1024
36+
maxHeight = 1024
37+
)
38+
39+
check := func(provider *Provider) bool {
40+
return provider.Internet == nil || len(provider.Internet.ImagePlaceholderServiceTemplateList) == 0
41+
}
42+
p := f.GetProviderWithCheck(check)
43+
if check(p) {
44+
return ""
45+
}
46+
47+
if width <= 0 || width > maxWidth {
48+
width = uint32(f.IntBetween(1, maxWidth))
49+
}
50+
51+
if height <= 0 || height > maxHeight {
52+
height = uint32(f.IntBetween(1, maxHeight))
53+
}
54+
55+
return f.Format(f.RandomStringElement(p.Internet.ImagePlaceholderServiceTemplateList), map[string]interface{}{
56+
"WIDTH": width,
57+
"HEIGHT": height,
58+
})
59+
}

providers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type AddressProvider struct {
2323
type InternetProvider struct {
2424
FreeEmailDomains, Tlds, UserNameFormatTemplates []string
2525
EmailFormatTemplate string
26+
ImagePlaceholderServiceTemplateList []string
2627
}
2728

2829
type PhoneNumberProvider struct {
@@ -72,6 +73,12 @@ func (f *Faker) InitProviderMap() {
7273
"{{.FirstName}}##", "?{{.LastName}}",
7374
},
7475
EmailFormatTemplate: "{{.UserName}}@{{.EmailDomain}}",
76+
ImagePlaceholderServiceTemplateList: []string{
77+
//"https://www.lorempixel.com/{{.WIDTH}}/{{.HEIGHT}}",
78+
//"https://dummyimage.com/{{.WIDTH}}/{{.HEIGHT}}",
79+
"https://placekitten.com/{{.WIDTH}}/{{.HEIGHT}}",
80+
"https://placeimg.com/{{.WIDTH}}/{{.HEIGHT}}/any",
81+
},
7582
},
7683
Person: &PersonProvider{
7784
NameFormatTemplate: "{{.FirstName}}{{.LastName}}",

0 commit comments

Comments
 (0)