discord / lilliput

Resize images and animated GIFs in Go
https://discord.com/blog/how-discord-resizes-150-million-images-every-day-with-go-and-c
Other
1.96k stars 123 forks source link

fix: Improve fit and resize functions to handle dimensions consistently. #180

Closed skidder closed 2 months ago

skidder commented 2 months ago

This PR modifies both the fit and resize functions to handle dimensions more consistently, with fit using a new calculation method and resize simplifying to always use the exact requested dimensions. This behavior regressed with the changes #178 , which this PR restores.

Changes:

Behavior Examples:

  1. Fit function (new logic):

    a. Both dimensions smaller than source: Original: 800x600, Requested: 400x300 Result: 400x300 (scales down, maintaining aspect ratio, may crop)

    b. Square fit larger than original: Original: 800x600, Requested: 1000x1000 Result: 600x600 (fits within original dimensions)

    c. Both dimensions larger than original: Original: 800x600, Requested: 1000x800 Result: 800x600 (maintains original size)

    d. Mixed scaling: Original: 800x600, Requested: 1000x400 Result: 1000x400 (allows partial upscaling, maintains aspect ratio, may crop)

  2. Resize function (updated behavior): For all cases: Always resizes to exact requested dimensions Example: Original: 800x600, Requested: 1000x750 Result: 1000x750 (resizes to exact dimensions, may change aspect ratio)

The new fit logic ensures that:

The updated resize function now always resizes to the exact requested dimensions, potentially changing the aspect ratio.

These changes provide more predictable and consistent behavior for both functions, with fit focusing on maintaining aspect ratio (with potential cropping) and resize always producing the exact requested dimensions.