parent
20c585a621
commit
a793c4fe22
|
@ -10,7 +10,7 @@ import (
|
|||
"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
|
||||
img, _, err := image.Decode(bytes.NewReader(inputImage))
|
||||
if err != nil {
|
||||
|
@ -21,11 +21,11 @@ func AddStatusIndicatorToImage(inputImage []byte, innerColor color.Color, indica
|
|||
width := img.Bounds().Max.X
|
||||
height := img.Bounds().Max.Y
|
||||
|
||||
indicatorOuterRadius := (indicatorSize / 2) + indicatorBorder
|
||||
indicatorRadius := indicatorSize / 2
|
||||
|
||||
// calculate the center point
|
||||
x := float64(width) - indicatorCenterToEdge
|
||||
y := float64(height) - indicatorCenterToEdge
|
||||
x := float64(width) - indicatorRadius
|
||||
y := float64(height) - indicatorRadius
|
||||
|
||||
// create a new gg.Context instance
|
||||
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
|
||||
dc.SetColor(color.Transparent)
|
||||
for i := x - indicatorOuterRadius; i <= x+indicatorOuterRadius; i++ {
|
||||
for j := y - indicatorOuterRadius; j <= y+indicatorOuterRadius; j++ {
|
||||
if math.Pow(i-x, 2)+math.Pow(j-y, 2) <= math.Pow(indicatorOuterRadius, 2) {
|
||||
for i := x - indicatorRadius; i <= x+indicatorRadius; i++ {
|
||||
for j := y - indicatorRadius; j <= y+indicatorRadius; j++ {
|
||||
if math.Pow(i-x, 2)+math.Pow(j-y, 2) <= math.Pow(indicatorRadius, 2) {
|
||||
dc.SetPixel(int(i), int(j))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// draw inner circle
|
||||
dc.DrawCircle(x, y, indicatorOuterRadius-indicatorBorder)
|
||||
dc.DrawCircle(x, y, indicatorRadius-indicatorBorder)
|
||||
dc.SetColor(innerColor)
|
||||
dc.Fill()
|
||||
|
||||
|
|
|
@ -73,7 +73,6 @@ type ImageParams struct {
|
|||
RingWidth float64
|
||||
IndicatorSize float64
|
||||
IndicatorBorder float64
|
||||
IndicatorCenterToEdge float64
|
||||
IndicatorColor color.Color
|
||||
|
||||
AuthorID string
|
||||
|
@ -228,16 +227,6 @@ func ParseImageParams(logger *zap.Logger, params url.Values) ImageParams {
|
|||
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"]
|
||||
if len(ringWidthStrs) != 0 {
|
||||
ringWidth, err := strconv.ParseFloat(ringWidthStrs[0], 64)
|
||||
|
@ -350,7 +339,7 @@ func handleAccountImagesImpl(multiaccountsDB *multiaccounts.Database, logger *za
|
|||
if parsed.IndicatorSize != 0 {
|
||||
// enlarge indicator size based on identity image size / desired size
|
||||
// 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 {
|
||||
logger.Error("handleAccountImagesImpl: failed to draw status-indicator for initials", zap.Error(err))
|
||||
return
|
||||
|
@ -400,7 +389,7 @@ func handleAccountImagesPlaceholder(logger *zap.Logger, w http.ResponseWriter, p
|
|||
|
||||
if parsed.IndicatorSize != 0 {
|
||||
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 {
|
||||
logger.Error("handleAccountImagesPlaceholder: failed to draw status-indicator for initials", zap.Error(err))
|
||||
return
|
||||
|
@ -495,7 +484,7 @@ func handleAccountInitialsImpl(multiaccountsDB *multiaccounts.Database, logger *
|
|||
}
|
||||
|
||||
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 {
|
||||
logger.Error("failed to draw status-indicator for initials", zap.Error(err))
|
||||
return
|
||||
|
@ -536,7 +525,7 @@ func handleAccountInitialsPlaceholder(logger *zap.Logger, w http.ResponseWriter,
|
|||
}
|
||||
|
||||
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 {
|
||||
logger.Error("failed to draw status-indicator for initials", zap.Error(err))
|
||||
return
|
||||
|
@ -665,7 +654,7 @@ func handleContactImages(db *sql.DB, logger *zap.Logger) http.HandlerFunc {
|
|||
}
|
||||
|
||||
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 {
|
||||
logger.Error("handleAccountImagesImpl: failed to draw status-indicator for initials", zap.Error(err))
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue