Simplify thumbnail resizing logic to ensure target constraints are respected (#28012)
Automatic Merge
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
8c5f00da86
Коммит
70dc4888fd
@@ -26,32 +26,15 @@ func GeneratePreview(img image.Image, width int) image.Image {
|
||||
|
||||
// GenerateThumbnail generates the thumbnail for the given image.
|
||||
func GenerateThumbnail(img image.Image, targetWidth, targetHeight int) image.Image {
|
||||
thumb := img
|
||||
width := img.Bounds().Dx()
|
||||
height := img.Bounds().Dy()
|
||||
expectedRatio := float64(targetHeight) / float64(targetWidth)
|
||||
|
||||
// If both dimensions are over the target size, we scale down until one is not.
|
||||
// If one dimension is over the target size, we scale down until both are not.
|
||||
// If neither dimension is over the target size, we return the image as is.
|
||||
if height > targetHeight || width > targetWidth {
|
||||
ratio := float64(height) / float64(width)
|
||||
if ratio < expectedRatio {
|
||||
if height >= targetHeight {
|
||||
thumb = imaging.Resize(img, 0, targetHeight, imaging.Lanczos)
|
||||
} else {
|
||||
thumb = imaging.Resize(img, targetWidth, 0, imaging.Lanczos)
|
||||
}
|
||||
} else {
|
||||
if width >= targetWidth {
|
||||
thumb = imaging.Resize(img, targetWidth, 0, imaging.Lanczos)
|
||||
} else {
|
||||
thumb = imaging.Resize(img, 0, targetHeight, imaging.Lanczos)
|
||||
}
|
||||
}
|
||||
// We keep aspect ratio and ensure the output dimensions are never higher than the provided targets.
|
||||
if width > height {
|
||||
return imaging.Resize(img, targetWidth, 0, imaging.Lanczos)
|
||||
}
|
||||
|
||||
return thumb
|
||||
return imaging.Resize(img, 0, targetHeight, imaging.Lanczos)
|
||||
}
|
||||
|
||||
// GenerateMiniPreviewImage generates the mini preview for the given image.
|
||||
|
||||
Ссылка в новой задаче
Block a user