Skip to content

Commit

Permalink
Round sizes for more accurate calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthSim committed Jun 7, 2019
1 parent dbad387 commit b2c5e9c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions process.go
Expand Up @@ -191,8 +191,8 @@ func transformImage(ctx context.Context, img *vipsImage, data []byte, po *proces

scale := calcScale(widthToScale, heightToScale, po, imgtype)

cropWidth = int(float64(cropWidth) * scale)
cropHeight = int(float64(cropHeight) * scale)
cropWidth = roundToInt(float64(cropWidth) * scale)
cropHeight = roundToInt(float64(cropHeight) * scale)

if scale != 1 && data != nil && canScaleOnLoad(imgtype, scale) {
if imgtype == imageTypeWEBP || imgtype == imageTypeSVG {
Expand All @@ -212,8 +212,8 @@ func transformImage(ctx context.Context, img *vipsImage, data []byte, po *proces
// Update scale after scale-on-load
newWidth, newHeight, _, _ := extractMeta(img)

widthToScale = int(float64(widthToScale) * float64(newWidth) / float64(srcWidth))
heightToScale = int(float64(heightToScale) * float64(newHeight) / float64(srcHeight))
widthToScale = roundToInt(float64(widthToScale) * float64(newWidth) / float64(srcWidth))
heightToScale = roundToInt(float64(heightToScale) * float64(newHeight) / float64(srcHeight))

scale = calcScale(widthToScale, heightToScale, po, imgtype)
}
Expand Down Expand Up @@ -264,8 +264,8 @@ func transformImage(ctx context.Context, img *vipsImage, data []byte, po *proces

checkTimeout(ctx)

dprWidth := int(float64(po.Width) * po.Dpr)
dprHeight := int(float64(po.Height) * po.Dpr)
dprWidth := roundToInt(float64(po.Width) * po.Dpr)
dprHeight := roundToInt(float64(po.Height) * po.Dpr)

cropGravity := po.Crop.Gravity
if cropGravity.Type == gravityUnknown {
Expand Down
6 changes: 6 additions & 0 deletions utils.go
@@ -1,5 +1,7 @@
package main

import "math"

func maxInt(a, b int) int {
if a > b {
return a
Expand All @@ -13,3 +15,7 @@ func minInt(a, b int) int {
}
return b
}

func roundToInt(a float64) int {
return int(math.Round(a))
}

0 comments on commit b2c5e9c

Please sign in to comment.