Only shrink when resizing with fit (#803)

This commit is contained in:
Arne Beer 2019-09-25 01:54:14 +02:00 committed by Vincent Prouillet
parent b16d525134
commit f96aad2fdd
3 changed files with 16 additions and 4 deletions

View file

@ -41,8 +41,8 @@ pub enum ResizeOp {
/// Scales the image to a specified height with width computed such /// Scales the image to a specified height with width computed such
/// that aspect ratio is preserved /// that aspect ratio is preserved
FitHeight(u32), FitHeight(u32),
/// Scales the image such that it fits within the specified width and /// If the image is larger than the specified width or height, scales the image such
/// height preserving aspect ratio. /// that it fits within the specified width and height preserving aspect ratio.
/// Either dimension may end up being smaller, but never larger than specified. /// Either dimension may end up being smaller, but never larger than specified.
Fit(u32, u32), Fit(u32, u32),
/// Scales the image such that it fills the specified width and height. /// 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), Scale(w, h) => img.resize_exact(w, h, RESIZE_FILTER),
FitWidth(w) => img.resize(w, u32::max_value(), RESIZE_FILTER), FitWidth(w) => img.resize(w, u32::max_value(), RESIZE_FILTER),
FitHeight(h) => img.resize(u32::max_value(), h, 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) => { Fill(w, h) => {
let factor_w = img_w as f32 / w as f32; let factor_w = img_w as f32 / w as f32;
let factor_h = img_h as f32 / h as f32; let factor_h = img_h as f32 / h as f32;

View file

@ -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") }} {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=0, height=150, op="fit_height") }}
### **`"fit"`** ### **`"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 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. 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(..., width=150, height=150, op="fit")`
{{ resize_image(path="documentation/content/image-processing/01-zola.png", width=150, height=150, op="fit") }} {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=150, height=150, op="fit") }}

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB