img_comp 1.0.3

jpg, png 图片压缩
Documentation
use rimage::image::{imageops::FilterType, DynamicImage, GenericImageView};
use std::fs;
use std::path::Path;

pub(crate) fn resize_by_width(image: DynamicImage, width: u32) -> anyhow::Result<DynamicImage> {
    let (w, h) = image.dimensions();
    if width >= w {
        // 当要改变的宽度,比实际宽度大时,就不修改
        return Ok(image);
    }
    let scale = (width as f64) / (w as f64);
    let new_w = width;
    let new_h = ((h as f64) * scale) as u32;
    let img_out = image.resize(new_w, new_h, FilterType::Lanczos3);
    Ok(img_out)
}

pub(crate) fn create_directory_if_not_exists(path: &str) -> anyhow::Result<()> {
    let path = Path::new(path);
    if !path.exists() {
        fs::create_dir_all(path)?;
    }
    Ok(())
}