From b2c5e9c4ab5f044b1d105231abe5646d941ee14d Mon Sep 17 00:00:00 2001 From: DarthSim Date: Sat, 8 Jun 2019 00:24:48 +0600 Subject: [PATCH] Round sizes for more accurate calculations --- process.go | 12 ++++++------ utils.go | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/process.go b/process.go index f794546a30..62a91432be 100644 --- a/process.go +++ b/process.go @@ -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 { @@ -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) } @@ -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 { diff --git a/utils.go b/utils.go index 26e3c296af..d6b880c8ec 100644 --- a/utils.go +++ b/utils.go @@ -1,5 +1,7 @@ package main +import "math" + func maxInt(a, b int) int { if a > b { return a @@ -13,3 +15,7 @@ func minInt(a, b int) int { } return b } + +func roundToInt(a float64) int { + return int(math.Round(a)) +}