Skip to content

Commit

Permalink
Add Arabic Support to Avatar Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
BasselArt committed Dec 29, 2024
1 parent f3a0a52 commit 24479c8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
9 changes: 8 additions & 1 deletion app/handlers/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,32 @@ func LetterAvatar() web.HandlerFunc {
// URL decode the name
decodedName, err := url.QueryUnescape(name)
if err != nil {
log.Error(c, err)
return c.Failure(err)
}

size, err := c.QueryParamAsInt("size")
if err != nil {
log.Error(c, err)
return c.BadRequest(web.Map{})
}
size = between(size, 50, 200)

img, err := letteravatar.Draw(size, letteravatar.Extract(decodedName), &letteravatar.Options{
extractedLetter := letteravatar.Extract(decodedName)
log.Debugf(c, "Generating letter avatar for name '%s', extracted letter: '%s'", decodedName, extractedLetter)

img, err := letteravatar.Draw(size, extractedLetter, &letteravatar.Options{
PaletteKey: fmt.Sprintf("%s:%s", id, decodedName),
})
if err != nil {
log.Error(c, err)
return c.Failure(err)
}

buf := new(bytes.Buffer)
err = png.Encode(buf, img)
if err != nil {
log.Error(c, err)
return c.Failure(err)
}

Expand Down
14 changes: 3 additions & 11 deletions app/pkg/letteravatar/letteravatar.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package letteravatar

import (
"embed"
"image"
"image/color"
"image/draw"
Expand All @@ -12,11 +11,9 @@ import (
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
)

//go:embed fonts
var fonts embed.FS

// Options contains the options for letter avatar generation
type Options struct {
PaletteKey string
Expand Down Expand Up @@ -53,13 +50,8 @@ func Draw(size int, text string, opts *Options) (image.Image, error) {
// Draw the background
draw.Draw(img, img.Bounds(), &image.Uniform{bg}, image.Point{}, draw.Src)

// Load the font
fontBytes, err := fonts.ReadFile("fonts/IBMPlexSansArabic-Regular.ttf")
if err != nil {
return nil, err
}

f, err := truetype.Parse(fontBytes)
// Use the built-in Go font for now
f, err := truetype.Parse(goregular.TTF)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 24479c8

Please sign in to comment.