A library for resize and compress images to jpg.
mpsc::Sender
(see Using Message Passing to Transfer Data Between Threads in rust tutorial).Visit image crate page. This crate uses image crate for opening image files.
FolderCompressor
and its compress
function example.The function compress all images in given origin folder with multithread at the same time,
and wait until everything is done. This function does not using any mpsc::Sender
.
```rust
use std::path::PathBuf;
use std::sync::mpsc;
use imagecompressor::FolderCompressor;
use imagecompressor::Factor;
let origin = PathBuf::from("origindir"); // original directory path
let dest = PathBuf::from("destdir"); // destination directory path
let thread_count = 4; // number of threads
let mut comp = FolderCompressor::new(origin, dest); comp.setcalfunc(|width, height, filesize| {return Factor::new(75., 0.7)}); //example closure comp.setthreadcount(4); match comp.compress(){ Ok() => {}, Err(e) => println!("Cannot compress the folder!: {}", e), } ```
FolderCompressor
and its compress_with_sender
example.The function compress all images in given origin folder with multithread at the same time, and wait until everything is done.
With mpsc::Sender
(argument tx
in this example),
the process running in this function will dispatch a message indicating whether image compression is complete.
```rust
use std::path::PathBuf;
use std::sync::mpsc;
use imagecompressor::FolderCompressor;
use imagecompressor::Factor;
let origin = PathBuf::from("origindir"); // original directory path
let dest = PathBuf::from("destdir"); // destination directory path
let thread_count = 4; // number of threads
let (tx, tr) = mpsc::channel(); // Sender and Receiver. for more info, check mpsc and message passing.
let mut comp = FolderCompressor::new(origin, dest); comp.setcalfunc(|width, height, filesize| {return Factor::new(75., 0.7)}); //example closure comp.setthreadcount(4); match comp.compresswithsender(tx.clone()) { Ok() => {}, Err(e) => println!("Cannot compress the folder!: {}", e), } ```
Compressor
and compress_to_jpg
example.Compressing just a one image.
rust
use std::path::PathBuf;
use image_compressor::compressor::Compressor;
use image_compressor::Factor;
let origin = PathBuf::from("origin").join("file1.jpg");
let dest = PathBuf::from("dest");
let comp = Compressor::new(origin_dir, dest_dir, |width, height, file_size| {return Factor::new(75., 0.7)});
comp.compress_to_jpg();