adjust padding for QR Images (#3510)

This commit is contained in:
Siddarth Kumar 2023-06-01 13:41:01 +05:30 committed by GitHub
parent a7df4ed388
commit d37b605796
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 20 deletions

View file

@ -1 +1 @@
0.154.0
0.154.1

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

@ -155,14 +155,40 @@ func EncodePNG(img *image.RGBA) ([]byte, error) {
return resultImg.Bytes(), nil
}
func CreateCircle(img image.Image) *image.RGBA {
func CreateCircleWithPadding(img image.Image, padding int) *image.RGBA {
bounds := img.Bounds()
circle := image.NewRGBA(bounds)
draw.DrawMask(circle, bounds, img, image.ZP, &Circle{
X: bounds.Dx() / 2,
Y: bounds.Dy() / 2,
R: bounds.Dx() / 2,
}, image.ZP, draw.Over)
width := bounds.Dx()
// only relying on width as a metric here because we know that we
// store profile images in a perfect circle
radius := width / 2
paddedWidth := width + 2*padding
paddedRadius := paddedWidth / 2
// Create a new circular image with padding
newBounds := image.Rect(0, 0, paddedWidth, paddedWidth)
circle := image.NewRGBA(newBounds)
// Create a larger circular mask for the padding
paddingMask := &Circle{
X: paddedRadius,
Y: paddedRadius,
R: paddedRadius,
}
// Draw the white color onto the circle with padding mask
draw.DrawMask(circle, circle.Bounds(), image.NewUniform(color.White), image.ZP, paddingMask, image.ZP, draw.Src)
// Create a new circle mask with the original size
circleMask := &Circle{
X: radius,
Y: radius,
R: radius,
}
// Draw the original image onto the white circular image at the center (with padding offset)
draw.DrawMask(circle, bounds.Add(image.Pt(padding, padding)), img, image.ZP, circleMask, image.ZP, draw.Over)
return circle
}

File diff suppressed because one or more lines are too long

View file

@ -188,10 +188,8 @@ func ToLogoImageFromBytes(imageBytes []byte, padding int) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("decoding image failed: %v", err)
}
paddedImg := images.AddPadding(img, padding)
circle := images.CreateCircle(img)
centeredImg := images.PlaceCircleInCenter(paddedImg, circle)
resultBytes, err := images.EncodePNG(centeredImg)
circle := images.CreateCircleWithPadding(img, padding)
resultBytes, err := images.EncodePNG(circle)
if err != nil {
return nil, fmt.Errorf("encoding PNG failed: %v", err)
}
@ -214,11 +212,13 @@ func GetLogoImage(multiaccountsDB *multiaccounts.Database, keyUID string, imageN
return nil, err
}
// default padding to 10 to make the QR with profile image look as per
// the designs
padding = 10
if identityImageObjectFromDB == nil {
padding = GetPadding(staticImageData)
LogoBytes, err = ToLogoImageFromBytes(staticImageData, padding)
} else {
padding = GetPadding(identityImageObjectFromDB.Payload)
LogoBytes, err = ToLogoImageFromBytes(identityImageObjectFromDB.Payload, padding)
}

View file

@ -107,6 +107,7 @@ func (s *QROpsTestSuite) TestQROpsCodeWithSuperImposingLogo() {
payload := generateQRBytes(params, s.Logger, db)
s.Require().NotEmpty(payload)
expectedPayload, err := images.Asset("_assets/tests/qr/QRWithLogo.png")
require.Equal(s.T(), payload, expectedPayload)
s.Require().NoError(err)