Skip to content

Commit 7d1f7c9

Browse files
committed
Use RoundToEven when calculating offsets and watermark size
1 parent 168f6f6 commit 7d1f7c9

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

imath/imath.go

+8
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,11 @@ func Shrink(a int, shrink float64) int {
5858

5959
return Round(float64(a) / shrink)
6060
}
61+
62+
func ShrinkToEven(a int, shrink float64) int {
63+
if a == 0 {
64+
return 0
65+
}
66+
67+
return RoundToEven(float64(a) / shrink)
68+
}

options/processing_options.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ func defaultProcessingOptions(headers http.Header) (*ProcessingOptions, error) {
11131113
}
11141114
if len(headerWidth) > 0 {
11151115
if w, err := strconv.Atoi(headerWidth); err == nil {
1116-
po.Width = imath.Scale(w, 1/po.Dpr)
1116+
po.Width = imath.Shrink(w, po.Dpr)
11171117
}
11181118
}
11191119
}

processing/calc_position.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package processing
22

33
import (
4+
"math"
5+
46
"github.com/imgproxy/imgproxy/v3/imath"
57
"github.com/imgproxy/imgproxy/v3/options"
68
)
79

810
func calcPosition(width, height, innerWidth, innerHeight int, gravity *options.GravityOptions, dpr float64, allowOverflow bool) (left, top int) {
911
if gravity.Type == options.GravityFocusPoint {
10-
pointX := imath.Scale(width, gravity.X)
11-
pointY := imath.Scale(height, gravity.Y)
12+
pointX := imath.ScaleToEven(width, gravity.X)
13+
pointY := imath.ScaleToEven(height, gravity.Y)
1214

1315
left = pointX - innerWidth/2
1416
top = pointY - innerHeight/2
1517
} else {
16-
offX, offY := int(gravity.X*dpr), int(gravity.Y*dpr)
18+
offX, offY := int(math.RoundToEven(gravity.X*dpr)), int(math.RoundToEven(gravity.Y*dpr))
1719

18-
left = (width-innerWidth+1)/2 + offX
19-
top = (height-innerHeight+1)/2 + offY
20+
left = imath.ShrinkToEven(width-innerWidth+1, 2) + offX
21+
top = imath.ShrinkToEven(height-innerHeight+1, 2) + offY
2022

2123
if gravity.Type == options.GravityNorth || gravity.Type == options.GravityNorthEast || gravity.Type == options.GravityNorthWest {
2224
top = 0 + offY

processing/padding.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ func padding(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptio
1212
return nil
1313
}
1414

15-
paddingTop := imath.Scale(po.Padding.Top, pctx.dprScale)
16-
paddingRight := imath.Scale(po.Padding.Right, pctx.dprScale)
17-
paddingBottom := imath.Scale(po.Padding.Bottom, pctx.dprScale)
18-
paddingLeft := imath.Scale(po.Padding.Left, pctx.dprScale)
15+
paddingTop := imath.ScaleToEven(po.Padding.Top, pctx.dprScale)
16+
paddingRight := imath.ScaleToEven(po.Padding.Right, pctx.dprScale)
17+
paddingBottom := imath.ScaleToEven(po.Padding.Bottom, pctx.dprScale)
18+
paddingLeft := imath.ScaleToEven(po.Padding.Left, pctx.dprScale)
1919

2020
return img.Embed(
2121
img.Width()+paddingLeft+paddingRight,

processing/watermark.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func prepareWatermark(wm *vips.Image, wmData *imagedata.ImageData, opts *options
3232
po.Format = wmData.Type
3333

3434
if opts.Scale > 0 {
35-
po.Width = imath.Max(imath.Scale(imgWidth, opts.Scale), 1)
36-
po.Height = imath.Max(imath.Scale(imgHeight, opts.Scale), 1)
35+
po.Width = imath.Max(imath.ScaleToEven(imgWidth, opts.Scale), 1)
36+
po.Height = imath.Max(imath.ScaleToEven(imgHeight, opts.Scale), 1)
3737
}
3838

3939
if opts.Replicate {

0 commit comments

Comments
 (0)