avatarbuilder

package
v0.0.0-...-8964234 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: MIT Imports: 11 Imported by: 0

README

avatar builder

Using go free_type to build default avatar with string

Usage

You can reference ./example

Some snippet is as blow

  // init avatarbuilder, you need to tell builder ttf file and how to alignment text
	ab := avatarbuilder.NewAvatarBuilder("./SourceHanSansSC-Medium.ttf", &calc.SourceHansSansSCMedium{})
	ab.SetBackgroundColorHex(colors[1])
	ab.SetFrontgroundColor(color.White)
	ab.SetFontSize(80)
	ab.SetAvatarSize(200, 200)
	if err := ab.GenerateImageAndSave("哈哈哈", "./out.png"); err != nil {
		fmt.Println(err)
		return
	}

Extend Other Font

Because element of width of each font is different, so you need tell avatar how to align the content. Avatar already implement a free font(made by google and adobe)'s center algorithm in ./calc, If you need other font, feel free to PR or issue.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AvatarBuilder

type AvatarBuilder struct {
	W int
	H int
	// contains filtered or unexported fields
}

func NewAvatarBuilder

func NewAvatarBuilder(fontFile string, calc FontCenterCalculator) *AvatarBuilder

func NewAvatarBuilderWithOption

func NewAvatarBuilderWithOption(fontFile string, calc FontCenterCalculator, opt ...BuilderOption) *AvatarBuilder

func (*AvatarBuilder) GenerateImage

func (ab *AvatarBuilder) GenerateImage(s string) ([]byte, error)

func (*AvatarBuilder) GenerateImageAndSave

func (ab *AvatarBuilder) GenerateImageAndSave(s string, outName string) error

func (*AvatarBuilder) GetFontWidth

func (ab *AvatarBuilder) GetFontWidth() int

func (*AvatarBuilder) SetAvatarSize

func (ab *AvatarBuilder) SetAvatarSize(w int, h int)

func (*AvatarBuilder) SetBackGroundColor

func (ab *AvatarBuilder) SetBackGroundColor(c color.Color)

func (*AvatarBuilder) SetBackGroundColorHex

func (ab *AvatarBuilder) SetBackGroundColorHex(hex uint32)

func (*AvatarBuilder) SetFontSize

func (ab *AvatarBuilder) SetFontSize(size float64)

func (*AvatarBuilder) SetFrontGroundColor

func (ab *AvatarBuilder) SetFrontGroundColor(c color.Color)

func (*AvatarBuilder) SetFrontGroundColorHex

func (ab *AvatarBuilder) SetFrontGroundColorHex(hex uint32)

type BuilderOption

type BuilderOption func(*AvatarBuilder)

func SetAvatarSize

func SetAvatarSize(w int, h int) BuilderOption

func SetBackGroundColor

func SetBackGroundColor(c color.Color) BuilderOption

func SetBackGroundColorHex

func SetBackGroundColorHex(hex uint32) BuilderOption

func SetFontSize

func SetFontSize(size float64) BuilderOption

func SetFrontGroundColor

func SetFrontGroundColor(c color.Color) BuilderOption

func SetFrontGroundColorHex

func SetFrontGroundColorHex(hex uint32) BuilderOption

type FontCenterCalculator

type FontCenterCalculator interface {
	// CalculateCenterLocation used to calculate center location in different font style
	CalculateCenterLocation(string, *AvatarBuilder) (int, int)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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