1
0
Эх сурвалжийг харах

Crop before resize in Fill function

Charlie Collard 6 жил өмнө
parent
commit
2fe522e994
1 өөрчлөгдсөн 7 нэмэгдсэн , 4 устгасан
  1. 7 4
      resize.go

+ 7 - 4
resize.go

@@ -292,14 +292,17 @@ func Fill(img image.Image, width, height int, anchor Anchor, filter ResampleFilt
 	srcAspectRatio := float64(srcW) / float64(srcH)
 	minAspectRatio := float64(minW) / float64(minH)
 
-	var tmp *image.NRGBA
+	var scale float64
 	if srcAspectRatio < minAspectRatio {
-		tmp = Resize(img, minW, 0, filter)
+		scale = float64(srcW) / float64(minW)
 	} else {
-		tmp = Resize(img, 0, minH, filter)
+		scale = float64(srcH) / float64(minH)
 	}
+	cropW := int(float64(minW) * scale)
+	cropH := int(float64(minH) * scale)
+	tmp := CropAnchor(img, cropW, cropH, anchor)
 
-	return CropAnchor(tmp, minW, minH, anchor)
+	return Resize(tmp, minW, minH, filter)
 }
 
 // Thumbnail scales the image up or down using the specified resample filter, crops it