image_optimizer/optimization/
webp_optimizer.rs1use anyhow::Result;
2use image::DynamicImage;
3use std::fs;
4use std::path::Path;
5
6use crate::cli::Cli;
7
8pub fn optimize_webp(
13 input_path: &Path,
14 output_path: &Path,
15 args: &Cli,
16 resized_img: Option<DynamicImage>,
17) -> Result<()> {
18 let rgb_img = if let Some(img) = resized_img {
19 img.to_rgb8()
20 } else {
21 image::open(input_path)?.to_rgb8()
22 };
23
24 let encoder = if args.lossless {
25 webp::Encoder::from_rgb(&rgb_img, rgb_img.width(), rgb_img.height()).encode_lossless()
26 } else {
27 webp::Encoder::from_rgb(&rgb_img, rgb_img.width(), rgb_img.height())
28 .encode(f32::from(args.quality))
29 };
30
31 fs::write(output_path, &*encoder)?;
32
33 Ok(())
34}