From 4d6171a4b3a3a8cd9885805daeddd26468333ff4 Mon Sep 17 00:00:00 2001 From: Karim-W Date: Sat, 4 Mar 2023 18:14:12 +0400 Subject: [PATCH] feat. configurable quality and size --- src/cli.rs | 2 ++ src/compressor.rs | 13 ++++++++++--- src/main.rs | 7 ++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index c5f6c7b..485080d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -8,6 +8,8 @@ use clap::Parser; pub struct Cli { pub source: PathBuf, pub destination: PathBuf, + pub quality: f32, + pub size:f32, } impl Cli { diff --git a/src/compressor.rs b/src/compressor.rs index 66f5f44..a17e158 100644 --- a/src/compressor.rs +++ b/src/compressor.rs @@ -4,11 +4,18 @@ use image_compressor::Factor; pub struct Compress { pub paths: Vec, pub destination: PathBuf, + pub quality: f32, + pub size: f32, } impl Compress { - pub fn new(paths: Vec, destination: PathBuf) -> Self { - Self { paths, destination } + pub fn new( + paths: Vec, + destination: PathBuf, + quality: f32, + size: f32 + ) -> Self { + Self { paths, destination, quality, size } } pub fn compress(&self) { println!("compressing {:?} to {:?}", @@ -21,7 +28,7 @@ impl Compress { path, &self.destination, ); - compressor.set_factor(Factor::new(90.0, 1.0)); + compressor.set_factor(Factor::new(self.quality, self.size)); match compressor.compress_to_jpg(){ Ok(_) => {}, Err(e) => println!("Cannot compress file {:?} !: {}", path,e), diff --git a/src/main.rs b/src/main.rs index fb7dce3..3c34e5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,11 @@ fn main() { } } } - let compressor = compressor::Compress::new(paths, args.destination); + let compressor = compressor::Compress::new( + paths, + args.destination, + args.quality, + args.size, + ); compressor.compress(); }