use image::imageops;
pub const DEFAULT_FONT_RATIO: f64 = 11.0 / 24.0;
pub trait CustomRatioResize {
fn resize_custom_ratio(
&self,
width: Option<u32>,
height: Option<u32>,
font_ratio: f64,
filter: imageops::FilterType,
) -> image::DynamicImage;
}
impl CustomRatioResize for image::DynamicImage {
fn resize_custom_ratio(
&self,
width: Option<u32>,
height: Option<u32>,
font_ratio: f64,
filter: imageops::FilterType,
) -> image::DynamicImage {
let (new_width, new_height) = match (width, height) {
(None, None) => return self.to_owned(),
(None, Some(height)) => (
calc_new_width(height, self.width(), self.height(), font_ratio),
height,
),
(Some(width), None) => (
width,
calc_new_height(width, self.width(), self.height(), font_ratio),
),
(Some(width), Some(height)) => (width, height),
};
self.resize_exact(new_width, new_height, filter)
}
}
pub fn calc_new_width(new_height: u32, width: u32, height: u32, font_ratio: f64) -> u32 {
((new_height * width) as f64 / (height as f64 * font_ratio)) as u32
}
pub fn calc_new_height(new_width: u32, width: u32, height: u32, font_ratio: f64) -> u32 {
(new_width as f64 * font_ratio * height as f64 / width as f64) as u32
}