1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-18 11:12:10 +02:00

More accurate scale calculation

This commit is contained in:
DarthSim 2019-09-16 23:09:28 +06:00
parent eb28352a78
commit 40fe09b15e

View File

@ -39,15 +39,24 @@ func extractMeta(img *vipsImage) (int, int, int, bool) {
} }
func calcScale(width, height int, po *processingOptions, imgtype imageType) float64 { func calcScale(width, height int, po *processingOptions, imgtype imageType) float64 {
var scale float64 var shrink float64
srcW, srcH := float64(width), float64(height) srcW, srcH := float64(width), float64(height)
dstW, dstH := float64(po.Width), float64(po.Height)
if (po.Width == 0 || po.Width == width) && (po.Height == 0 || po.Height == height) { if po.Width == 0 {
scale = 1 dstW = srcW
}
if po.Height == 0 {
dstH = srcH
}
if dstW == srcW && dstH == srcH {
shrink = 1
} else { } else {
wr := float64(po.Width) / srcW wshrink := srcW / dstW
hr := float64(po.Height) / srcH hshrink := srcH / dstH
rt := po.Resize rt := po.Resize
@ -64,31 +73,31 @@ func calcScale(width, height int, po *processingOptions, imgtype imageType) floa
switch { switch {
case po.Width == 0: case po.Width == 0:
scale = hr shrink = hshrink
case po.Height == 0: case po.Height == 0:
scale = wr shrink = wshrink
case rt == resizeFit: case rt == resizeFit:
scale = math.Min(wr, hr) shrink = math.Max(wshrink, hshrink)
default: default:
scale = math.Max(wr, hr) shrink = math.Min(wshrink, hshrink)
} }
} }
if !po.Enlarge && scale > 1 && imgtype != imageTypeSVG { if !po.Enlarge && shrink < 1 && imgtype != imageTypeSVG {
scale = 1 shrink = 1
} }
scale *= po.Dpr shrink /= po.Dpr
if srcW*scale < 1 { if shrink > srcW {
scale = 1 / srcW shrink = srcW
} }
if srcH*scale < 1 { if shrink > srcH {
scale = 1 / srcH shrink = srcH
} }
return scale return 1.0 / shrink
} }
func canScaleOnLoad(imgtype imageType, scale float64) bool { func canScaleOnLoad(imgtype imageType, scale float64) bool {