A Golang lib to generate placeholder images with text
MIT License
A Golang lib to generate placeholder images with text.
Make sure you have a working Go environment (Go 1.9 or higher is required). See the install instructions.
To install sticker, simply run:
go get github.com/muesli/sticker
To compile it from source:
cd $GOPATH/src/github.com/muesli/sticker
go get -u -v
go build && go test -v
gen, err := sticker.NewImageGenerator(sticker.Options{
TTFPath: "/usr/share/fonts/TTF/Roboto-Bold.ttf",
MarginRatio: 0.2,
Foreground: color.RGBA{0x96, 0x96, 0x96, 0xff},
Background: color.RGBA{0xcc, 0xcc, 0xcc, 0xff},
BackgroundImage: img,
})
img, err := gen.NewPlaceholder("Lorem ipsum!", 400, 200)
...
You can also pass a []byte
containing the font:
import "golang.org/x/image/font/gofont/gomono"
gen, err := sticker.NewImageGenerator(sticker.Options{
TTF: gomono.TTF,
MarginRatio: 0.2,
Foreground: color.RGBA{0x96, 0x96, 0x96, 0xff},
Background: color.RGBA{0xcc, 0xcc, 0xcc, 0xff},
})
sticker will automatically pick a suitable font size, so the text will always fit neatly inside the image.
If you supply a background image, it will automatically be cropped and scaled (while maintaining its original aspect ratio) to the desired output size.