mod file_utility;
use std::fs::create_dir_all;
use std::path::Path;
use image::imageops::FilterType;
use image::DynamicImage;
use file_utility::{
file_folder_from_path,
file_ext_from_path,
file_name_from_path,
open_image,
};
fn resize_image(src_folder: &str, dest_folder: &str, folder_name: &str, img: &DynamicImage, file_dir_path: &str, out_file_name: &str) {
let dest1_folder = Path::new(&file_dir_path.replace(src_folder, &format!("{}/{}", dest_folder, folder_name))).join("");
let dest1 = dest1_folder.join(out_file_name);
create_dir_all(dest1_folder).unwrap();
if img.width() > 1920 || img.height() > 1080 {
img.resize(1920, 1080, FilterType::CatmullRom).save(dest1).unwrap();
} else {
img.save(dest1).unwrap();
}
}
fn resize_image_from_file(file_path: &str, dest_folder: &str, out_file_name: &str, width: u32, height: u32) {
let ext = file_ext_from_path(file_path);
let img = open_image(&file_path);
let dest1 = Path::new(dest_folder).join(format!("{}.{}", out_file_name, ext));
create_dir_all(dest_folder).unwrap();
if img.width() > width || img.height() > height {
img.resize(width, height, FilterType::CatmullRom).save(dest1).unwrap();
} else {
img.save(dest1).unwrap();
}
}
pub fn resize_as_ext(file_path: &str, dest_folder: &str, out_ext_name: &str, width: u32, height: u32) {
let file_dir_path = file_folder_from_path(&file_path);
let file_name = file_name_from_path(file_path);
let ext_name = &file_ext_from_path(&file_name);
let out_file_name = &file_name.replace(&format!(".{}", ext_name), "");
let img = open_image(&file_path);
let dest1 = Path::new(dest_folder).join(format!("{}.{}", out_file_name, out_ext_name));
create_dir_all(dest_folder).unwrap();
if img.width() > width || img.height() > height {
img.resize(width, height, FilterType::CatmullRom).save(dest1).unwrap();
} else {
img.save(dest1).unwrap();
}
}
pub struct Size {
name: String,
width: u32,
height: u32,
}
impl Size {
pub fn new(name: String, width: u32, height: u32) -> Size{
Self{
name: name,
width: width,
height: height,
}
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
}
pub fn resize_image_to_multi_sizes(file_path: &str, dest_folder: &str) {
let sizes = vec!(
Size{ name: String::from("xxs"), width: 300, height: 300 },
Size{ name: String::from("xs"), width: 320, height: 480 },
Size{ name: String::from("s"), width: 480, height: 720 },
Size{ name: String::from("m"), width: 768, height: 1024 },
Size{ name: String::from("l"), width: 1024, height: 1366 },
Size{ name: String::from("xl"), width: 1920, height: 1080 },
Size{ name: String::from("xxl"), width: 3840, height: 2160 },
Size{ name: String::from("ultra"), width: 7680, height: 4320 },
);
resize_image_to_multi_sizes_custom(file_path, dest_folder, sizes);
}
pub fn resize_image_to_multi_sizes_custom(file_path: &str, dest_folder: &str, sizes: Vec<Size>) {
for size in sizes {
resize_image_from_file(file_path, dest_folder, &size.name, size.width, size.height);
}
}
pub fn resize_image_by_code(file_path: &str, dest_folder: &str, code: &str) {
match get_size_by_code(code) {
Some(size) => {
resize_image_from_file(file_path, dest_folder, &size.name, size.width, size.height);
},
None => {
eprintln!("Invalid image size code");
},
}
}
pub fn get_size_by_code(code: &str) -> Option<Size> {
match code {
"xxs" => {
Some(Size{ name: String::from("xxs"), width: 300, height: 300 })
},
"xs" => {
Some(Size{ name: String::from("xs"), width: 320, height: 480 })
},
"s" => {
Some(Size{ name: String::from("s"), width: 480, height: 720 })
},
"m" => {
Some(Size{ name: String::from("m"), width: 768, height: 1024 })
},
"l" => {
Some(Size{ name: String::from("l"), width: 1024, height: 1366 })
},
"xl" => {
Some(Size{ name: String::from("xl"), width: 1920, height: 1080 })
},
"xxl" => {
Some(Size{ name: String::from("xxl"), width: 3840, height: 2160 })
},
"ultra" => {
Some(Size{ name: String::from("ultra"), width: 7680, height: 4320 })
},
&_ => {
None
},
}
}