mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
resources/image: Fix fill with smartcrop sometimes returning 0 bytes images
Fixes #7955
This commit is contained in:
@@ -15,6 +15,7 @@ package images
|
||||
|
||||
import (
|
||||
"image"
|
||||
"math"
|
||||
|
||||
"github.com/disintegration/gift"
|
||||
|
||||
@@ -41,6 +42,14 @@ type imagingResizer struct {
|
||||
}
|
||||
|
||||
func (r imagingResizer) Resize(img image.Image, width, height uint) image.Image {
|
||||
// See https://github.com/gohugoio/hugo/issues/7955#issuecomment-861710681
|
||||
scaleX, scaleY := calcFactorsNfnt(width, height, float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))
|
||||
if width == 0 {
|
||||
width = uint(math.Ceil(float64(img.Bounds().Dx()) / scaleX))
|
||||
}
|
||||
if height == 0 {
|
||||
height = uint(math.Ceil(float64(img.Bounds().Dy()) / scaleY))
|
||||
}
|
||||
result, _ := r.p.Filter(img, gift.Resize(int(width), int(height), r.filter))
|
||||
return result
|
||||
}
|
||||
@@ -71,3 +80,25 @@ func (p *ImageProcessor) smartCrop(img image.Image, width, height int, filter gi
|
||||
|
||||
return img.Bounds().Intersect(rect), nil
|
||||
}
|
||||
|
||||
// Calculates scaling factors using old and new image dimensions.
|
||||
// Code borrowed from https://github.com/nfnt/resize/blob/83c6a9932646f83e3267f353373d47347b6036b2/resize.go#L593
|
||||
func calcFactorsNfnt(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
|
||||
if width == 0 {
|
||||
if height == 0 {
|
||||
scaleX = 1.0
|
||||
scaleY = 1.0
|
||||
} else {
|
||||
scaleY = oldHeight / float64(height)
|
||||
scaleX = scaleY
|
||||
}
|
||||
} else {
|
||||
scaleX = oldWidth / float64(width)
|
||||
if height == 0 {
|
||||
scaleY = scaleX
|
||||
} else {
|
||||
scaleY = oldHeight / float64(height)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user