2024-04-26 19:30:35 +00:00
|
|
|
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/
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
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/
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
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 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return "https://picsum.photos/" + strconv.Itoa(width) + "/" + strconv.Itoa(height)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Image generates a random rgba image
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func Image(width int, height int) *img.RGBA { return image(globalFaker.Rand, width, height) }
|
|
|
|
|
|
|
|
// Image generates a random rgba image
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
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 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
upLeft := img.Point{0, 0}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
lowRight := img.Point{width, height}
|
|
|
|
|
|
|
|
img := img.NewRGBA(img.Rectangle{upLeft, lowRight})
|
|
|
|
|
|
|
|
// Set color for each pixel
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
for x := 0; x < width; x++ {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
for y := 0; y < height; y++ {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
img.Set(x, y, imgCol.RGBA{uint8(number(r, 0, 255)), uint8(number(r, 0, 255)), uint8(number(r, 0, 255)), 0xff})
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return img
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImageJpeg generates a random rgba jpeg image
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func ImageJpeg(width int, height int) []byte { return imageJpeg(globalFaker.Rand, width, height) }
|
|
|
|
|
|
|
|
// ImageJpeg generates a random rgba jpeg image
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
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 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
jpeg.Encode(buf, image(r, width, height), nil)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return buf.Bytes()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImagePng generates a random rgba png image
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func ImagePng(width int, height int) []byte { return imagePng(globalFaker.Rand, width, height) }
|
|
|
|
|
|
|
|
// ImagePng generates a random rgba png image
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
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 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
png.Encode(buf, image(r, width, height))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return buf.Bytes()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addImageLookup() {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
AddFuncLookup("imageurl", Info{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Display: "Image URL",
|
|
|
|
|
|
|
|
Category: "image",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Description: "Web address pointing to an image file that can be accessed and displayed online",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Example: "https://picsum.photos/500/500",
|
|
|
|
|
|
|
|
Output: "string",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Params: []Param{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
{Field: "width", Display: "Width", Type: "int", Default: "500", Description: "Image width in px"},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
{Field: "height", Display: "Height", Type: "int", Default: "500", Description: "Image height in px"},
|
|
|
|
},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
width, err := info.GetInt(m, "width")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if width < 10 || width >= 1000 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, errors.New("invalid image width, must be greater than 10, less than 1000")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
height, err := info.GetInt(m, "height")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if height < 10 || height >= 1000 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, errors.New("invalid image height, must be greater than 10, less than 1000")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return imageURL(r, width, height), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
AddFuncLookup("imagejpeg", Info{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Display: "Image JPEG",
|
|
|
|
|
|
|
|
Category: "image",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Description: "Image file format known for its efficient compression and compatibility",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Example: "file.jpeg - bytes",
|
|
|
|
|
|
|
|
Output: "[]byte",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
ContentType: "image/jpeg",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Params: []Param{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
{Field: "width", Display: "Width", Type: "int", Default: "500", Description: "Image width in px"},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
{Field: "height", Display: "Height", Type: "int", Default: "500", Description: "Image height in px"},
|
|
|
|
},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
width, err := info.GetInt(m, "width")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if width < 10 || width >= 1000 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, errors.New("invalid image width, must be greater than 10, less than 1000")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
height, err := info.GetInt(m, "height")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if height < 10 || height >= 1000 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, errors.New("invalid image height, must be greater than 10, less than 1000")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return imageJpeg(r, width, height), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
AddFuncLookup("imagepng", Info{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Display: "Image PNG",
|
|
|
|
|
|
|
|
Category: "image",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Description: "Image file format known for its lossless compression and support for transparency",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Example: "file.png - bytes",
|
|
|
|
|
|
|
|
Output: "[]byte",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
ContentType: "image/png",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Params: []Param{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
{Field: "width", Display: "Width", Type: "int", Default: "500", Description: "Image width in px"},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
{Field: "height", Display: "Height", Type: "int", Default: "500", Description: "Image height in px"},
|
|
|
|
},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
width, err := info.GetInt(m, "width")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if width < 10 || width >= 1000 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, errors.New("invalid image width, must be greater than 10, less than 1000")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
height, err := info.GetInt(m, "height")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if height < 10 || height >= 1000 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, errors.New("invalid image height, must be greater than 10, less than 1000")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return imagePng(r, width, height), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
},
|
|
|
|
})
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|