mdavatar

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2020 License: MIT Imports: 17 Imported by: 0

README

MDAvatar

MDAvatar

MDAvatar 可以根据字符串生成单字符头像,并且可以高度自定义,支持生成中文头像,圆形头像

Features

  • 自定义头像 text 处理方式
  • 自定义图片 size
  • 自定义字体
    • 自带默认字体,字体颜色为白色,暂不支持自定义字体颜色
    • 当字体设置支持中文时,头像 text 也可以渲染中文
  • 自定义头像背景颜色列表
    • 自带默认颜色列表 Material Design Colors
    • 当没有设置 background 时,默认是随机获取颜色列表中的一种 RGBA
  • 自定义背景
    • 当设置 background 后,不会启用随机颜色作为 background
    • background 也是一种 RGBA
  • 支持多种图形生成
    • 正方形,默认
    • 圆形

Installation

  • Require go version >= 1.13
  • Require go mod enable
$ go get -u github.com/laojianzi/mdavatar

Examples

package main

import (
	"fmt"
	"image/png"
	"log"
	"os"
	"time"

	"github.com/laojianzi/mdavatar"
)

func main() {
	avatar, err := mdavatar.New("MDAvatar").Build()
	if err != nil {
		log.Fatal(err)
	}

	filename := fmt.Sprintf("out-%d.png", time.Now().Unix())
	file, err := os.Create(filename)
	if err != nil {
		log.Fatal(err)
	}
	
	if err := png.Encode(file, avatar); err != nil {
		log.Fatal(err)
	}
}

TODO

  • 支持 cli (生成 png/jpg)
  • 支持自定义形状 (圆形、椭圆形、方形 ...)
  • 支持多种返回形式 (HTTP、Base64、WriteToFile ...)

Prior Art

项目参考了一些现有的思路或者实现等

License

This project is licensed under the MIT License.

License can be found here.

Documentation

Overview

Ref: https://blog.logrocket.com/working-with-go-images/ Ref: https://github.com/zdhxiong/mdclub/tree/master/src/Vendor

Package mdavatar generated by go-bindata.// sources: static/NotoSansSC-Regular.otf static/Roboto-Light.ttf

Index

Constants

This section is empty.

Variables

View Source
var DefaultColors = mdColors

DefaultColors colors on mdavatar default

View Source
var DefaultConfig = &Config{
	avatarTextHandle:  DefaultAvatarTextHandle,
	avatarSize:        256,
	colors:            DefaultColors,
	letterFontContent: MustAsset("static/Roboto-Light.ttf"),
	asianFontContent:  MustAsset("static/NotoSansSC-Regular.otf"),
}

DefaultConfig config on mdavatar default can be used directly

Functions

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("nonexistent") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func DefaultAvatarTextHandle

func DefaultAvatarTextHandle(text string, enableAsianFontChar bool) string

DefaultAvatarTextHandle mdavatar default handler for avatar text

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

Types

type AvatarTextHandler

type AvatarTextHandler func(s string, enableAsianFontChar bool) string

AvatarTextHandler handle for avatar text allow custom !!! e.g:

text: laojianzi => L
text: 老健仔 => L

type Config

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

Config on mdavatar build

func New

func New(text string, opts ...Option) *Config

New return a mdavatar config (*Config)

func (*Config) Build

func (config *Config) Build() (*image.RGBA, error)

Build generate a image from config

func (*Config) Builds added in v1.0.1

func (config *Config) Builds(newStyle style.MDAvatarBuildStyleNewFunc) (*image.RGBA, error)

Builds generate a image from config by a build style

func (*Config) MakeBackground

func (config *Config) MakeBackground() (*image.RGBA, error)

Background new a background image from Config.avatarSize and Config.colors

type Option

type Option func(*Config)

Option set field value for Config

func DisableAsianFontChar

func DisableAsianFontChar() Option

DisableAsianFontChar set Config.enableAsianFontChar is false and reset Config.asianFont is empty

func WithAsianFont

func WithAsianFont(asianFont string) Option

WithAsianFont with option for Config.asianFont field

func WithAvatarSize

func WithAvatarSize(avatarSize int) Option

WithAvatarSize with option for Config.avatarSize field

func WithAvatarTextHandle

func WithAvatarTextHandle(avatarTextHandle AvatarTextHandler) Option

WithAvatarTextHandle with option for Config.avatarTextHandle field

func WithBackground

func WithBackground(background *image.RGBA) Option

WithBackground with option for Config.background field

func WithColors

func WithColors(colors []color.RGBA) Option

WithColors with option for Config.colors field

func WithLetterFont

func WithLetterFont(letterFont string) Option

WithLetterFont with option for Config.letterFont field

func WithPadding

func WithPadding(padding int) Option

WithPadding with option for Config.padding field

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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