From d907dd16e0af9fab60c2b8ce60963870cf1d79e2 Mon Sep 17 00:00:00 2001 From: "C. David" <290046085+cv1d@users.noreply.github.com> Date: Sun, 13 Sep 2026 14:06:55 +0800 Subject: [PATCH 1/2] Add DynamicImage::thumbnail_to_fill --- src/images/dynimage.rs | 52 +++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/src/images/dynimage.rs b/src/images/dynimage.rs index 594325dd56..bd64fdf7bd 100644 --- a/src/images/dynimage.rs +++ b/src/images/dynimage.rs @@ -1144,6 +1144,35 @@ impl DynamicImage { imageops::resize::resize_impl(self, nwidth, nheight, filter).unwrap() } + /// Resize this image using the specified filter algorithm. + /// The image's aspect ratio is preserved. + /// The image is scaled to the maximum possible size that fits + /// within the larger (relative to aspect ratio) of the bounds + /// specified by `nwidth` and `nheight`, then cropped to + /// fit within the other bound. + /// + /// This method operates on pixel channel values directly without taking into account color + /// space data. + pub fn resize_to_fill(&mut self, nwidth: u32, nheight: u32, filter: imageops::FilterType) { + let (width2, height2) = + resize_dimensions(self.width(), self.height(), nwidth, nheight, true); + self.resize_exact(width2, height2, filter); + + let (iwidth, iheight) = self.dimensions(); + let ratio = u64::from(iwidth) * u64::from(nheight); + let nratio = u64::from(nwidth) * u64::from(iheight); + + let select = if nratio > ratio { + let y = (iheight - nheight) / 2; + Rect::from_xy_ranges(0..nwidth, y..y + nheight) + } else { + let x = (iwidth - nwidth) / 2; + Rect::from_xy_ranges(x..x + nwidth, 0..nheight) + }; + + *self = self.crop(select); + } + /// Scale this image down to fit within a specific size. /// Returns a new image. The image's aspect ratio is preserved. /// The image is scaled to the maximum possible size that fits @@ -1165,6 +1194,7 @@ impl DynamicImage { /// Scale this image down to a specific size. /// Returns a new image. Does not preserve aspect ratio. /// `nwidth` and `nheight` are the new image's dimensions. + /// /// This method uses a fast integer algorithm where each source /// pixel contributes to exactly one target pixel. /// May give aliasing artifacts if new size is close to old size. @@ -1176,21 +1206,26 @@ impl DynamicImage { dynamic_map!(*self, ref p => imageops::thumbnail(p, nwidth, nheight)) } - /// Resize this image using the specified filter algorithm. - /// The image's aspect ratio is preserved. + /// Scale this image down to fit within a specific size. + /// Returns a new image. The image's aspect ratio is preserved. /// The image is scaled to the maximum possible size that fits /// within the larger (relative to aspect ratio) of the bounds /// specified by `nwidth` and `nheight`, then cropped to /// fit within the other bound. /// + /// This method uses a fast integer algorithm where each source + /// pixel contributes to exactly one target pixel. + /// May give aliasing artifacts if new size is close to old size. + /// /// This method operates on pixel channel values directly without taking into account color /// space data. - pub fn resize_to_fill(&mut self, nwidth: u32, nheight: u32, filter: imageops::FilterType) { + #[must_use] + pub fn thumbnail_to_fill(&self, nwidth: u32, nheight: u32) -> DynamicImage { let (width2, height2) = resize_dimensions(self.width(), self.height(), nwidth, nheight, true); - self.resize_exact(width2, height2, filter); + let thumbnail = self.thumbnail_exact(width2, height2); - let (iwidth, iheight) = self.dimensions(); + let (iwidth, iheight) = thumbnail.dimensions(); let ratio = u64::from(iwidth) * u64::from(nheight); let nratio = u64::from(nwidth) * u64::from(iheight); @@ -1202,7 +1237,7 @@ impl DynamicImage { Rect::from_xy_ranges(x..x + nwidth, 0..nheight) }; - *self = self.crop(select); + self.crop(select) } /// Performs a Gaussian blur on this image. @@ -2506,13 +2541,14 @@ mod test { img.resize_exact(32, 32, crate::imageops::FilterType::Lanczos3); img }, - &|img| img.thumbnail(8, 8), - &|img| img.thumbnail_exact(8, 8), &|img| { let mut img = img.clone(); img.resize_to_fill(32, 32, crate::imageops::FilterType::Lanczos3); img }, + &|img| img.thumbnail(8, 8), + &|img| img.thumbnail_exact(8, 8), + &|img| img.thumbnail_to_fill(8, 8), &|img| img.blur(1.0), &|img| { img.blur_advanced( From 8e8d18e384f103cd71f10b663a50c42298ac3dd3 Mon Sep 17 00:00:00 2001 From: "C. David" <290046085+cv1d@users.noreply.github.com> Date: Sun, 13 Sep 2026 14:18:04 +0800 Subject: [PATCH 2/2] Fix incorrect cropping subject in DynamicImage::thumbnail_to_fill --- src/images/dynimage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/images/dynimage.rs b/src/images/dynimage.rs index bd64fdf7bd..e290bb585f 100644 --- a/src/images/dynimage.rs +++ b/src/images/dynimage.rs @@ -1237,7 +1237,7 @@ impl DynamicImage { Rect::from_xy_ranges(x..x + nwidth, 0..nheight) }; - self.crop(select) + thumbnail.crop(select) } /// Performs a Gaussian blur on this image.