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 697b117 commit f3a0a52
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
13 changes: 9 additions & 4 deletions app/handlers/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"image/color"
"image/png"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/getfider/fider/app/models/cmd"
"github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/query"

"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/crypto"
"github.com/getfider/fider/app/pkg/env"
Expand All @@ -32,14 +31,20 @@ func LetterAvatar() web.HandlerFunc {
name = "?"
}

// URL decode the name
decodedName, err := url.QueryUnescape(name)
if err != nil {
return c.Failure(err)
}

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

img, err := letteravatar.Draw(size, letteravatar.Extract(name), &letteravatar.Options{
PaletteKey: fmt.Sprintf("%s:%s", id, name),
img, err := letteravatar.Draw(size, letteravatar.Extract(decodedName), &letteravatar.Options{
PaletteKey: fmt.Sprintf("%s:%s", id, decodedName),
})
if err != nil {
return c.Failure(err)
Expand Down
9 changes: 5 additions & 4 deletions app/pkg/letteravatar/letteravatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"image/draw"
"math"
"strings"
"unicode/utf8"

"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
Expand All @@ -32,13 +33,13 @@ func Extract(name string) string {
return "?"
}

firstWord := []rune(words[0])
if len(firstWord) == 0 {
// Get first rune from first word
r, _ := utf8.DecodeRuneInString(words[0])
if r == utf8.RuneError {
return "?"
}

firstLetter := string(firstWord[0])
return firstLetter
return string(r)
}

// Draw generates a letter avatar image with the given size and text
Expand Down

0 comments on commit f3a0a52

Please sign in to comment.