summaryrefslogtreecommitdiffstats
path: root/modules/avatar
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-08-09 11:46:10 +0800
committerUnknwon <u@gogs.io>2015-08-09 11:46:10 +0800
commit4b43ffc96cf16cd49b5796feca8ad853f3e3a8ee (patch)
tree85306f0d5e79394a0dcc4dca4c1cd5e3256471ce /modules/avatar
parent56a8d573b0346dce9f6843d61997e5794bc16e4e (diff)
downloadgitea-4b43ffc96cf16cd49b5796feca8ad853f3e3a8ee.tar.gz
gitea-4b43ffc96cf16cd49b5796feca8ad853f3e3a8ee.zip
Generate random avatar based on e-mail when disable Gravatar
Diffstat (limited to 'modules/avatar')
-rw-r--r--modules/avatar/avatar.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/modules/avatar/avatar.go b/modules/avatar/avatar.go
index 73daa213c9..5aef6e02cd 100644
--- a/modules/avatar/avatar.go
+++ b/modules/avatar/avatar.go
@@ -19,9 +19,11 @@ import (
"errors"
"fmt"
"image"
+ "image/color/palette"
"image/jpeg"
"image/png"
"io"
+ "math/rand"
"net/http"
"net/url"
"os"
@@ -30,6 +32,7 @@ import (
"sync"
"time"
+ "github.com/issue9/identicon"
"github.com/nfnt/resize"
"github.com/gogits/gogs/modules/log"
@@ -59,6 +62,27 @@ func HashEmail(email string) string {
return hex.EncodeToString(h.Sum(nil))
}
+const _RANDOM_AVATAR_SIZE = 200
+
+// RandomImage generates and returns a random avatar image.
+func RandomImage(data []byte) (image.Image, error) {
+ randExtent := len(palette.WebSafe) - 32
+ rand.Seed(time.Now().UnixNano())
+ colorIndex := rand.Intn(randExtent)
+ backColorIndex := colorIndex - 1
+ if backColorIndex < 0 {
+ backColorIndex = randExtent - 1
+ }
+
+ // Size, background, forecolor
+ imgMaker, err := identicon.New(_RANDOM_AVATAR_SIZE,
+ palette.WebSafe[backColorIndex], palette.WebSafe[colorIndex:colorIndex+32]...)
+ if err != nil {
+ return nil, err
+ }
+ return imgMaker.Make(data), nil
+}
+
// Avatar represents the avatar object.
type Avatar struct {
Hash string