Fix image resizing calculation to use square root for scaling factor

This commit is contained in:
2025-06-25 19:32:05 +08:00
parent ef62c0cfe9
commit 8f57365975

View File

@@ -5,6 +5,7 @@ import (
"errors" "errors"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"image" "image"
"math"
"net/http" "net/http"
"nysoure/server/dao" "nysoure/server/dao"
"nysoure/server/model" "nysoure/server/model"
@@ -232,7 +233,7 @@ func getOrCreateResampledImage(i model.Image) ([]byte, error) {
return imgData, nil // No need to resample if the image is small enough return imgData, nil // No need to resample if the image is small enough
} }
scale := float64(resampledMaxPixels) / float64(pixels) scale := math.Sqrt(float64(resampledMaxPixels) / float64(pixels))
dstWidth := int(float64(img.Bounds().Dx()) * scale) dstWidth := int(float64(img.Bounds().Dx()) * scale)
dstHeight := int(float64(img.Bounds().Dy()) * scale) dstHeight := int(float64(img.Bounds().Dy()) * scale)
dstImg := imaging.Resize(img, dstWidth, dstHeight, imaging.Lanczos) dstImg := imaging.Resize(img, dstWidth, dstHeight, imaging.Lanczos)