Revert "fix: user avatar status indicator position (#3967)" (#3988)

This commit is contained in:
yqrashawn 2023-09-05 21:57:40 +08:00 committed by GitHub
parent 20c585a621
commit a793c4fe22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 42 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, indicatorCenterToEdge float64) ([]byte, error) { func AddStatusIndicatorToImage(inputImage []byte, innerColor color.Color, indicatorSize, indicatorBorder 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
indicatorOuterRadius := (indicatorSize / 2) + indicatorBorder indicatorRadius := indicatorSize / 2
// calculate the center point // calculate the center point
x := float64(width) - indicatorCenterToEdge x := float64(width) - indicatorRadius
y := float64(height) - indicatorCenterToEdge y := float64(height) - indicatorRadius
// 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 - indicatorOuterRadius; i <= x+indicatorOuterRadius; i++ { for i := x - indicatorRadius; i <= x+indicatorRadius; i++ {
for j := y - indicatorOuterRadius; j <= y+indicatorOuterRadius; j++ { for j := y - indicatorRadius; j <= y+indicatorRadius; j++ {
if math.Pow(i-x, 2)+math.Pow(j-y, 2) <= math.Pow(indicatorOuterRadius, 2) { if math.Pow(i-x, 2)+math.Pow(j-y, 2) <= math.Pow(indicatorRadius, 2) {
dc.SetPixel(int(i), int(j)) dc.SetPixel(int(i), int(j))
} }
} }
} }
// draw inner circle // draw inner circle
dc.DrawCircle(x, y, indicatorOuterRadius-indicatorBorder) dc.DrawCircle(x, y, indicatorRadius-indicatorBorder)
dc.SetColor(innerColor) dc.SetColor(innerColor)
dc.Fill() dc.Fill()

View File

@ -56,25 +56,24 @@ 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
IndicatorCenterToEdge float64 IndicatorColor color.Color
IndicatorColor color.Color
AuthorID string AuthorID string
URL string URL string
@ -228,16 +227,6 @@ 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)
@ -350,7 +339,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, parsed.IndicatorCenterToEdge*enlargeRatio) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize*enlargeRatio, parsed.IndicatorBorder*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
@ -400,7 +389,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, parsed.IndicatorCenterToEdge) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize*enlargeIndicatorRatio, parsed.IndicatorBorder*enlargeIndicatorRatio)
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
@ -495,7 +484,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, parsed.IndicatorCenterToEdge) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize, parsed.IndicatorBorder)
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
@ -536,7 +525,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, parsed.IndicatorCenterToEdge) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize, parsed.IndicatorBorder)
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
@ -665,7 +654,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, parsed.IndicatorCenterToEdge*enlargeRatio) payload, err = images.AddStatusIndicatorToImage(payload, parsed.IndicatorColor, parsed.IndicatorSize*enlargeRatio, parsed.IndicatorBorder*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