Struct image_compressor::FolderCompressor
source · pub struct FolderCompressor { /* private fields */ }Expand description
Compressor struct for a directory.
Implementations
sourceimpl FolderCompressor
impl FolderCompressor
sourcepub fn new<O: AsRef<Path>, D: AsRef<Path>>(origin_path: O, dest_path: D) -> Self
pub fn new<O: AsRef<Path>, D: AsRef<Path>>(origin_path: O, dest_path: D) -> Self
Create a new FolderCompressor instance.
Just needs original directory path and destination directory path.
If you do not set the quality calculation function,
it will use the default calculation function which sets the quality only by the file size.
Likewise, if you do not set the number of threads, only one thread is used by default.
Examples
use image_compressor::FolderCompressor;
use std::path::Path;
let origin = Path::new("origin");
let dest = Path::new("dest");
let comp = FolderCompressor::new(origin, dest);sourcepub fn set_cal_func(&mut self, cal_func: fn(_: u32, _: u32, _: u64) -> Factor)
pub fn set_cal_func(&mut self, cal_func: fn(_: u32, _: u32, _: u64) -> Factor)
Setter for calculation function that return a Factor using to compress images.
Examples
use image_compressor::FolderCompressor;
use image_compressor::Factor;
use std::path::Path;
let origin = Path::new("origin");
let dest = Path::new("dest");
let mut comp = FolderCompressor::new(origin, dest);
comp.set_cal_func(|width, height, file_size| {return Factor::new(75., 0.7)});sourcepub fn set_delelte_origin(&mut self, to_delete: bool)
pub fn set_delelte_origin(&mut self, to_delete: bool)
Set whether to delete original files.
pub fn set_sender(&mut self, sender: Sender<String>)
sourcepub fn set_thread_count(&mut self, thread_count: u32)
pub fn set_thread_count(&mut self, thread_count: u32)
Setter for the number of threads used to compress images.
Examples
use image_compressor::FolderCompressor;
use image_compressor::Factor;
use std::path::Path;
let origin = Path::new("origin");
let dest = Path::new("dest");
let mut comp = FolderCompressor::new(origin, dest);
comp.set_thread_count(4);sourcepub fn compress(self) -> Result<(), Box<dyn Error>>
pub fn compress(self) -> Result<(), Box<dyn Error>>
Folder compress function.
The function compress all images in given origin folder with multithreading, and wait until everything is done.
If user set a Sender for FolderCompressor before, the method sends messages whether compressing is complete.
Warning
Since this function comsume its self, the FolderCompressor instance (which is self) is no longer available after calling this function.
use std::path::PathBuf;
use std::sync::mpsc;
use image_compressor::FolderCompressor;
let origin = PathBuf::from("origin_dir");
let dest = PathBuf::from("dest_dir");
let (tx, tr) = mpsc::channel();
let mut comp = FolderCompressor::new(origin, dest);
comp.set_sender(tx);
comp.set_thread_count(4);
match comp.compress(){
Ok(_) => {},
Err(e) => println!("Cannot compress the folder!: {}", e),
}pub fn compress_with_sender(
self,
sender: Sender<String>
) -> Result<(), Box<dyn Error>>
compress method instead this