web_image/resize.rs
1use crate::error::WebImageError;
2use crate::image::WebImage;
3use image::imageops::FilterType;
4use image::DynamicImage;
5use wasm_bindgen::prelude::wasm_bindgen;
6
7#[wasm_bindgen]
8impl WebImage {
9 /// Resize this image while preserving aspect ratio. The image is scaled to the maximum
10 /// possible size that fits within the bounds specified by max_width and max_height.
11 ///
12 /// returns: Result<WebImage, WebImageError>
13 ///
14 /// # Errors
15 ///
16 /// Errors if conversion to DynamicImage fails
17 pub fn resize(self, max_width: u32, max_height: u32) -> Result<WebImage, WebImageError> {
18 let dynamic_image: DynamicImage = self.try_into()?;
19
20 Ok((&dynamic_image.resize(max_width, max_height, FilterType::Nearest)).into())
21 }
22
23 /// Resize this image while ignoring aspect ratio. The width and height provided become the
24 /// new dimensions for the image
25 ///
26 /// returns: Result<WebImage, WebImageError>
27 ///
28 /// # Errors
29 ///
30 /// Errors if conversion to DynamicImage fails
31 pub fn resize_exact(self, width: u32, height: u32) -> Result<WebImage, WebImageError> {
32 let dynamic_image: DynamicImage = self.try_into()?;
33
34 Ok((&dynamic_image.resize_exact(width, height, FilterType::Nearest)).into())
35 }
36
37 /// Resize this image while preserving aspect ratio. The image is scaled to the maximum
38 /// possible size that fits within the larger (relative to aspect ratio) of the bounds
39 /// specified by width and height, then cropped to fit within the other bound.
40 ///
41 /// returns: Result<WebImage, WebImageError>
42 ///
43 /// # Errors
44 ///
45 /// Errors if conversion to DynamicImage fails
46 pub fn resize_to_fill(self, width: u32, height: u32) -> Result<WebImage, WebImageError> {
47 let dynamic_image: DynamicImage = self.try_into()?;
48
49 Ok((&dynamic_image.resize_to_fill(width, height, FilterType::Nearest)).into())
50 }
51}