diff --git a/components/imageproc/src/lib.rs b/components/imageproc/src/lib.rs index b1f5827..7c754b5 100644 --- a/components/imageproc/src/lib.rs +++ b/components/imageproc/src/lib.rs @@ -41,8 +41,8 @@ pub enum ResizeOp { /// Scales the image to a specified height with width computed such /// that aspect ratio is preserved FitHeight(u32), - /// Scales the image such that it fits within the specified width and - /// height preserving aspect ratio. + /// If the image is larger than the specified width or height, scales the image such + /// that it fits within the specified width and height preserving aspect ratio. /// Either dimension may end up being smaller, but never larger than specified. Fit(u32, u32), /// Scales the image such that it fills the specified width and height. @@ -266,7 +266,13 @@ impl ImageOp { Scale(w, h) => img.resize_exact(w, h, RESIZE_FILTER), FitWidth(w) => img.resize(w, u32::max_value(), RESIZE_FILTER), FitHeight(h) => img.resize(u32::max_value(), h, RESIZE_FILTER), - Fit(w, h) => img.resize(w, h, RESIZE_FILTER), + Fit(w, h) => { + if img_w > w || img_h > h { + img.resize(w, h, RESIZE_FILTER) + } else { + img + } + }, Fill(w, h) => { let factor_w = img_w as f32 / w as f32; let factor_h = img_h as f32 / h as f32; diff --git a/docs/content/documentation/content/image-processing/index.md b/docs/content/documentation/content/image-processing/index.md index 8dbb281..c6973af 100644 --- a/docs/content/documentation/content/image-processing/index.md +++ b/docs/content/documentation/content/image-processing/index.md @@ -78,10 +78,16 @@ The source for all examples is this 300 × 380 pixels image: {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=0, height=150, op="fit_height") }} ### **`"fit"`** - Like `"fit_width"` and `"fit_height"` combined. + Like `"fit_width"` and `"fit_height"` combined, but only resize if the image is bigger than any of the specified dimensions. + This mode is handy, if e.g. images are automatically shrinked to certain sizes in a shortcode for mobile optimization. Resizes the image such that the result fits within `width` and `height` preserving aspect ratio. This means that both width or height will be at max `width` and `height`, respectively, but possibly one of them smaller so as to preserve the aspect ratio. + + `resize_image(..., width=5000, height=5000, op="fit")` + + {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=5000, height=5000, op="fit") }} + `resize_image(..., width=150, height=150, op="fit")` {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=150, height=150, op="fit") }} diff --git a/docs/static/processed_images/e8e628f3be10e77c00.png b/docs/static/processed_images/e8e628f3be10e77c00.png new file mode 100644 index 0000000..4cf656b Binary files /dev/null and b/docs/static/processed_images/e8e628f3be10e77c00.png differ