Browse Source

Only shrink when resizing with fit (#803)

index-subcmd
Arne Beer Vincent Prouillet 5 years ago
parent
commit
f96aad2fdd
3 changed files with 16 additions and 4 deletions
  1. +9
    -3
      components/imageproc/src/lib.rs
  2. +7
    -1
      docs/content/documentation/content/image-processing/index.md
  3. BIN
      docs/static/processed_images/e8e628f3be10e77c00.png

+ 9
- 3
components/imageproc/src/lib.rs 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
/// 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. /// 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;


+ 7
- 1
docs/content/documentation/content/image-processing/index.md 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") }}


BIN
docs/static/processed_images/e8e628f3be10e77c00.png View File

Before After
Width: 300  |  Height: 380  |  Size: 134KB

Loading…
Cancel
Save