sticker

package module
v0.0.0-...-82fe18b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2021 License: MIT Imports: 9 Imported by: 1

README

sticker

A Golang lib to generate placeholder images with text.

Installation

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

Example

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},
})

example placeholder example placeholder with background image

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.

Development

GoDoc Build Status Coverage Status Go ReportCard

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidDimensions gets returned when the requested image has an invalid width or height
	ErrInvalidDimensions = errors.New("values for width or height must be positive")

	// ErrMissingFontOption gets returned when there's no font specified in the options
	ErrMissingFontOption = errors.New("no font option specified")
)

Functions

This section is empty.

Types

type ImageGenerator

type ImageGenerator struct {
	// contains filtered or unexported fields
}

ImageGenerator lets you generate images & placeholders

func NewImageGenerator

func NewImageGenerator(options Options) (*ImageGenerator, error)

NewImageGenerator returns a new ImageGenerator

func (*ImageGenerator) NewPlaceholder

func (p *ImageGenerator) NewPlaceholder(text string, width, height int) (image.Image, error)

NewPlaceholder returns a placeholder image with the given text, width & height

type Options

type Options struct {
	TTFPath         string
	TTF             []byte
	Foreground      color.RGBA
	Background      color.RGBA
	BackgroundImage image.Image
	MarginRatio     float64
}

Options contains all the settings for an ImageGenerator

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL