img_craft 0.1.3

Utility of Images for General Purpose Application
Documentation
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();
    }
}

/// Resize an image and save as ext
/// 
/// # Overview
/// 
/// # Arguments
/// 
/// * `file_path` - The image file path
/// * `dest_folder` - The destination folder path
/// * `out_ext_name` - The image file extension
/// * `width` - The target size's width
/// * `height` - The target size's height
/// 
/// # Example
/// 
/// ```rust
/// use img_craft::{
///     resize_as_ext,
/// };
/// 
/// let file_path = "/home/user1/image1.jpg";
/// let dest_folder = "/home/user1/output";
/// let out_ext_name = "png";
/// let width = 480;
/// let height = 320;
/// resize_as_ext(file_path, dest_folder, out_ext_name, width, height);
/// ```
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
    }
}

/// Resize an Image to Multi-Resolution Images
/// 
/// # Overview
/// 
/// # Arguments
/// 
/// * `file_path` - The image file path
/// * `dest_folder` - The destination folder path
/// 
/// # Example
/// 
/// ```rust
/// use img_craft::{
///     resize_image_to_multi_sizes,
/// };
/// 
/// let file_path = "/home/user1/image1.jpg";
/// let dest_folder = "/home/user1/output";
/// resize_image_to_multi_sizes(file_path, dest_folder);
/// ```
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);
}

/// Resize an Image to Multi-Resolution Images with Customized Target Sizes
/// 
/// # Overview
/// 
/// # Arguments
/// 
/// * `file_path` - The image file path
/// * `dest_folder` - The destination folder path
/// * `sizes` - The target size vector
/// 
/// # Example
/// 
/// ```rust
/// use img_craft::{
///     resize_image_to_multi_sizes_custom,
///     Size,
/// };
/// 
/// let file_path = "/home/user1/image1.jpg";
/// let dest_folder = "/home/user1/output";
/// let sizes = vec!(
///   Size::new(String::from("xxs"),    300,  300),
/// );
/// 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);
    }
}

/// Resize an Image to specified Resolution by Code
/// 
/// # Arguments
/// 
/// # Overview
/// 
/// * `file_path` - The image file path
/// * `dest_folder` - The destination folder path
/// * `code` - One of {"xxs", "xs", "s", "m", "l", "xl", "xxl", "ultra"}
/// 
/// # Example
/// 
/// ```rust
/// use img_craft::{
///     resize_image_by_code,
/// };
/// 
/// let file_path = "/home/user1/image1.jpg";
/// let dest_folder = "/home/user1/output";
/// let code = "xs";
/// resize_image_by_code(file_path, dest_folder, code);
/// ```
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");
        },
    }
}

/// Get Size by Code
/// 
/// # Overview
/// 
/// # Arguments
/// 
/// * `code` - One of {"xxs", "xs", "s", "m", "l", "xl", "xxl", "ultra"}
/// 
/// # Example
/// 
/// ```rust
/// use img_craft::{
///     get_size_by_code,
/// };
/// 
/// let code = "xs";
/// let size: Size = get_size_by_code(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
        },
    }
}