diff options
| author | Jake Mannens <jake@asger.xyz> | 2026-06-25 02:22:34 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2026-06-25 02:23:47 +1000 |
| commit | d0d324ede91589d1e49a9a0728e5e5408262697b (patch) | |
| tree | 33efe3b3a0da345189925b0d6fd75993f518bebe /Services | |
| parent | 0f52838f8f983dae4750d824a1ee9a7c250dc914 (diff) | |
Added the ability to request arbitrary thumbnail ratios
Diffstat (limited to 'Services')
| -rw-r--r-- | Services/MediaService.cs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Services/MediaService.cs b/Services/MediaService.cs index 6019f3f..1cfc345 100644 --- a/Services/MediaService.cs +++ b/Services/MediaService.cs @@ -316,6 +316,22 @@ public class MediaService : IMediaService { if(height > image.Height) height = (int) image.Height; + // Crop the image if a different aspect ratio is specified + if(width is not null && height is not null) { + double requestedRatio = (double) width / (double) height; + double currentRatio = (double) image.Width / (double) image.Height; + + double w = image.Width; + double h = image.Height; + + if(requestedRatio < currentRatio) + w = w / currentRatio * requestedRatio; + if(requestedRatio > currentRatio) + h = h / requestedRatio * currentRatio; + + image.Extent((uint) w, (uint) h, Gravity.Center); + } + image.Thumbnail((uint) (width ?? -1), (uint) (height ?? -1)); image.Write(thumbPath, MagickFormat.Jpeg); |
