fix: user avatar status indicator position (#3989)

This commit is contained in:
yqrashawn 2023-09-08 09:07:24 +08:00 committed by GitHub
parent 15369f26dd
commit 623781481b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 31 deletions

View File

@ -10,7 +10,7 @@ import (
"github.com/fogleman/gg" "github.com/fogleman/gg"
) )
func AddStatusIndicatorToImage(inputImage []byte, innerColor color.Color, indicatorSize, indicatorBorder float64) ([]byte, error) { func AddStatusIndicatorToImage(inputImage []byte, innerColor color.Color, indicatorSize, indicatorBorder, indicatorCenterToEdge float64) ([]byte, error) {
// decode the input image // decode the input image
img, _, err := image.Decode(bytes.NewReader(inputImage)) img, _, err := image.Decode(bytes.NewReader(inputImage))
if err != nil { if err != nil {
@ -21,11 +21,11 @@ func AddStatusIndicatorToImage(inputImage []byte, innerColor color.Color, indica
width := img.Bounds().Max.X width := img.Bounds().Max.X
height := img.Bounds().Max.Y height := img.Bounds().Max.Y
indicatorRadius := indicatorSize / 2 indicatorOuterRadius := (indicatorSize / 2) + indicatorBorder
// calculate the center point // calculate the center point
x := float64(width) - indicatorRadius x := float64(width) - indicatorCenterToEdge
y := float64(height) - indicatorRadius y := float64(height) - indicatorCenterToEdge
// create a new gg.Context instance // create a new gg.Context instance
dc := gg.NewContext(width, height) dc := gg.NewContext(width, height)
@ -33,16 +33,16 @@ func AddStatusIndicatorToImage(inputImage []byte, innerColor color.Color, indica
// Loop through each pixel in the hole and set it to transparent // Loop through each pixel in the hole and set it to transparent
dc.SetColor(color.Transparent) dc.SetColor(color.Transparent)
for i := x - indicatorRadius; i <= x+indicatorRadius; i++ { for i := x - indicatorOuterRadius; i <= x+indicatorOuterRadius; i++ {
for j := y - indicatorRadius; j <= y+indicatorRadius; j++ { for j := y - indicatorOuterRadius; j <= y+indicatorOuterRadius; j++ {
if math.Pow(i-x, 2)+math.Pow(j-y, 2) <= math.Pow(indicatorRadius, 2) { if math.Pow(i-x, 2)+math.Pow(j-y, 2) <= math.Pow(indicatorOuterRadius, 2) {
dc.SetPixel(int(i), int(j)) dc.SetPixel(int(i), int(j))
} }
} }
} }
// draw inner circle // draw inner circle
dc.DrawCircle(x, y, indicatorRadius-indicatorBorder) dc.DrawCircle(x, y, indicatorOuterRadius-indicatorBorder)
dc.SetColor(innerColor) dc.SetColor(innerColor)
dc.Fill() dc.Fill()

View File

@ -56,24 +56,25 @@ func handleRequestDownloaderMissing(logger *zap.Logger) http.HandlerFunc {
} }
type ImageParams struct { type ImageParams struct {
KeyUID string KeyUID string
PublicKey string PublicKey string
ImageName string ImageName string
ImagePath string ImagePath string
FullName string FullName string
InitialsLength int InitialsLength int
FontFile string FontFile string
FontSize float64 FontSize float64
Color color.Color Color color.Color
BgSize int BgSize int
BgColor color.Color BgColor color.Color
UppercaseRatio float64 UppercaseRatio float64
Theme ring.Theme Theme ring.Theme
Ring bool Ring bool
RingWidth float64 RingWidth float64
IndicatorSize float64 IndicatorSize float64
IndicatorBorder float64 IndicatorBorder float64
IndicatorColor color.Color IndicatorCenterToEdge float64
IndicatorColor color.Color
AuthorID string AuthorID string
URL string URL string
@ -227,6 +228,16 @@ func ParseImageParams(logger *zap.Logger, params url.Values) ImageParams {
parsed.IndicatorBorder = indicatorBorder parsed.IndicatorBorder = indicatorBorder
} }
indicatorCenterToEdgeStrs := params["indicatorCenterToEdge"]
if len(indicatorCenterToEdgeStrs) != 0 {
indicatorCenterToEdge, err := strconv.ParseFloat(indicatorCenterToEdgeStrs[0], 64)
if err != nil {
logger.Error("ParseParams: invalid indicatorCenterToEdge", zap.String("indicatorCenterToEdge", indicatorCenterToEdgeStrs[0]))
indicatorCenterToEdge = 0
}
parsed.IndicatorCenterToEdge = indicatorCenterToEdge
}
ringWidthStrs := params["ringWidth"] ringWidthStrs := params["ringWidth"]
if len(ringWidthStrs) != 0 { if len(ringWidthStrs) != 0 {
ringWidth, err := strconv.ParseFloat(ringWidthStrs[0], 64) ringWidth, err := strconv.ParseFloat(ringWidthStrs[0], 64)
@ -339,7 +350,7 @@ func handleAccountImagesImpl(multiaccountsDB *multiaccounts.Database, logger *za
if parsed.IndicatorSize != 0 { if parsed.IndicatorSize != 0 {
// enlarge indicator size based on identity image size / desired size // enlarge indicator size based on identity image size / desired size
// or we get a bad quality identity image // or we get a bad quality identity image
payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize*enlargeRatio, parsed.IndicatorBorder*enlargeRatio) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize*enlargeRatio, parsed.IndicatorBorder*enlargeRatio, parsed.IndicatorCenterToEdge*enlargeRatio)
if err != nil { if err != nil {
logger.Error("handleAccountImagesImpl: failed to draw status-indicator for initials", zap.Error(err)) logger.Error("handleAccountImagesImpl: failed to draw status-indicator for initials", zap.Error(err))
return return
@ -389,7 +400,7 @@ func handleAccountImagesPlaceholder(logger *zap.Logger, w http.ResponseWriter, p
if parsed.IndicatorSize != 0 { if parsed.IndicatorSize != 0 {
enlargeIndicatorRatio := float64(width / parsed.BgSize) enlargeIndicatorRatio := float64(width / parsed.BgSize)
payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize*enlargeIndicatorRatio, parsed.IndicatorBorder*enlargeIndicatorRatio) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize*enlargeIndicatorRatio, parsed.IndicatorBorder*enlargeIndicatorRatio, parsed.IndicatorCenterToEdge)
if err != nil { if err != nil {
logger.Error("handleAccountImagesPlaceholder: failed to draw status-indicator for initials", zap.Error(err)) logger.Error("handleAccountImagesPlaceholder: failed to draw status-indicator for initials", zap.Error(err))
return return
@ -484,7 +495,7 @@ func handleAccountInitialsImpl(multiaccountsDB *multiaccounts.Database, logger *
} }
if parsed.IndicatorSize != 0 { if parsed.IndicatorSize != 0 {
payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize, parsed.IndicatorBorder) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize, parsed.IndicatorBorder, parsed.IndicatorCenterToEdge)
if err != nil { if err != nil {
logger.Error("failed to draw status-indicator for initials", zap.Error(err)) logger.Error("failed to draw status-indicator for initials", zap.Error(err))
return return
@ -525,7 +536,7 @@ func handleAccountInitialsPlaceholder(logger *zap.Logger, w http.ResponseWriter,
} }
if parsed.IndicatorSize != 0 { if parsed.IndicatorSize != 0 {
payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize, parsed.IndicatorBorder) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize, parsed.IndicatorBorder, parsed.IndicatorCenterToEdge)
if err != nil { if err != nil {
logger.Error("failed to draw status-indicator for initials", zap.Error(err)) logger.Error("failed to draw status-indicator for initials", zap.Error(err))
return return
@ -654,7 +665,7 @@ func handleContactImages(db *sql.DB, logger *zap.Logger) http.HandlerFunc {
} }
if parsed.IndicatorSize != 0 { if parsed.IndicatorSize != 0 {
payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize*enlargeRatio, parsed.IndicatorBorder*enlargeRatio) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize*enlargeRatio, parsed.IndicatorBorder*enlargeRatio, parsed.IndicatorCenterToEdge*enlargeRatio)
if err != nil { if err != nil {
logger.Error("handleAccountImagesImpl: failed to draw status-indicator for initials", zap.Error(err)) logger.Error("handleAccountImagesImpl: failed to draw status-indicator for initials", zap.Error(err))
return return