avatar

package module
v0.0.0-...-63e56b2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: Apache-2.0 Imports: 16 Imported by: 2

README

A V A T A R

Create avatars based on names. The colors are chosen based on the first character. You can save to disk ToDisk or send back over HTTP ToHTTP.

Example

package main

import (
	"github.com/ae0000/avatar"
)

func main() {
	avatar.ToDisk("AE", "../ae.png")

    // Which is the same as
    avatar.ToDisk("Andrew Edwards", "../ae.png")
}


Example

You can pass in a single character as well

// Single initial as well..
avatar.ToDisk("Jet", "../j.png")

Example

HTTP example

Using go-chi (highly recommended HTTP router)

package main

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/ae0000/avatar"
	"github.com/go-chi/chi"
)

func main() {
	r := chi.NewRouter()

	// Get the png based on the initials, You would use it like this:
	//    <img src="http://localhost:3000/avatar/ae/png" width="150">
	r.Get("/avatar/{initials}.png", func(w http.ResponseWriter, r *http.Request) {
		initials := chi.URLParam(r, "initials")

		avatar.ToHTTP(initials, w)
	})

	http.ListenAndServe(":3000", r)
}

TODO

  • HTTP example
  • Caching
  • Custom colors
  • Add unique colors that are missing (T-Z,0-9)

Documentation

Overview

Package avatar generates colorful avatar images from initials. It creates 500x500px PNG images with customizable colors and built-in caching.

Index

Constants

This section is empty.

Variables

View Source
var (
	Red      = image.Uniform{color.RGBA{230, 25, 75, 255}}
	Green    = image.Uniform{color.RGBA{60, 180, 75, 255}}
	Yellow   = image.Uniform{color.RGBA{255, 225, 25, 255}}
	Blue     = image.Uniform{color.RGBA{0, 130, 200, 255}}
	Orange   = image.Uniform{color.RGBA{245, 130, 48, 255}}
	Purple   = image.Uniform{color.RGBA{145, 30, 180, 255}}
	Cyan     = image.Uniform{color.RGBA{70, 240, 240, 255}}
	Magenta  = image.Uniform{color.RGBA{240, 50, 230, 255}}
	Lime     = image.Uniform{color.RGBA{210, 245, 60, 255}}
	Pink     = image.Uniform{color.RGBA{250, 190, 190, 255}}
	Teal     = image.Uniform{color.RGBA{0, 128, 128, 255}}
	Lavender = image.Uniform{color.RGBA{230, 190, 255, 255}}
	Brown    = image.Uniform{color.RGBA{170, 110, 40, 255}}
	Beige    = image.Uniform{color.RGBA{255, 250, 200, 255}}
	Maroon   = image.Uniform{color.RGBA{128, 0, 0, 255}}
	Mint     = image.Uniform{color.RGBA{170, 255, 195, 255}}
	Olive    = image.Uniform{color.RGBA{128, 128, 0, 255}}
	Coral    = image.Uniform{color.RGBA{255, 215, 180, 255}}
	Navy     = image.Uniform{color.RGBA{0, 0, 128, 255}}
	Grey     = image.Uniform{color.RGBA{128, 128, 128, 255}}
	Gold     = image.Uniform{color.RGBA{251, 184, 41, 255}}
)

Colors for background

Functions

func SetFontFacePath

func SetFontFacePath(f string)

SetFontFacePath sets the path to the TrueType font file to use for rendering avatar text. If not set, the default font "Roboto-Bold.ttf" will be used.

func ToDisk

func ToDisk(initials, path string) error

ToDisk generates an avatar image from the given initials and saves it as a PNG file at the specified path. The initials can be a full name (e.g., "John Doe" becomes "JD") or explicit initials (e.g., "AE"). Colors are automatically assigned based on the first character. Returns an error if avatar generation or file writing fails.

func ToDiskCustom

func ToDiskCustom(initials, path, bgColor, fontColor string) error

ToDiskCustom generates an avatar image with custom colors and saves it as a PNG file. The bgColor and fontColor parameters should be hex color strings (e.g., "#FF0000"). Empty strings use default colors. Returns an error if avatar generation or file writing fails.

func ToHTTP

func ToHTTP(initials string, w http.ResponseWriter) error

ToHTTP generates an avatar image from the given initials and writes it as a PNG to the provided http.ResponseWriter. Automatically sets appropriate headers including Content-Type, Cache-Control (30 days), and ETag for browser caching. Returns an error if avatar generation or writing fails.

func ToHTTPCustom

func ToHTTPCustom(initials, bgColor, fontColor string, w http.ResponseWriter) error

ToHTTPCustom generates an avatar image with custom colors and writes it as a PNG to the provided http.ResponseWriter. The bgColor and fontColor parameters should be hex color strings (e.g., "#FF0000"). Empty strings use default colors. Returns an error if avatar generation or writing fails.

Types

This section is empty.

Directories

Path Synopsis
examples
todisk command
tohttp command

Jump to

Keyboard shortcuts

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