Fix proportional scaling when single dimension is 0
When only width or height is specified (the other being 0), scale the image proportionally to maintain aspect ratio. Previously, 0 was passed directly to the resize function which produced a 0x0 image.
This commit is contained in:
@@ -66,10 +66,17 @@ func (p *ImageProcessor) Process(
|
|||||||
targetWidth := req.Size.Width
|
targetWidth := req.Size.Width
|
||||||
targetHeight := req.Size.Height
|
targetHeight := req.Size.Height
|
||||||
|
|
||||||
// If both are 0, keep original size
|
// Handle dimension calculation
|
||||||
if targetWidth == 0 && targetHeight == 0 {
|
if targetWidth == 0 && targetHeight == 0 {
|
||||||
|
// Both are 0: keep original size
|
||||||
targetWidth = origWidth
|
targetWidth = origWidth
|
||||||
targetHeight = origHeight
|
targetHeight = origHeight
|
||||||
|
} else if targetWidth == 0 {
|
||||||
|
// Only height specified: calculate width proportionally
|
||||||
|
targetWidth = origWidth * targetHeight / origHeight
|
||||||
|
} else if targetHeight == 0 {
|
||||||
|
// Only width specified: calculate height proportionally
|
||||||
|
targetHeight = origHeight * targetWidth / origWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resize if needed
|
// Resize if needed
|
||||||
|
|||||||
Reference in New Issue
Block a user