### Build and install the CLI tool Source: https://context7.com/starc007/image-compression/llms.txt Commands for cloning, building, and installing the project using Cargo. ```bash # Clone the repository git clone https://github.com/starc007/Image-compression.git cd Image-compression # Build debug version (faster compilation) cargo build # Build release version (optimized for performance) cargo build --release # Run directly with cargo cargo run -- --input ./photos --output ./compressed --quality 80 # Install globally (optional) cargo install --path . # The compiled binary location: # Debug: ./target/debug/image_compressor_cli # Release: ./target/release/image_compressor_cli ``` -------------------------------- ### Run the Image Compressor CLI Source: https://github.com/starc007/image-compression/blob/main/readme.md Basic usage syntax and a concrete example for executing the compressor with specific input, output, quality, and dimension parameters. ```bash image_compressor_cli [OPTIONS] ``` ```bash ./target/release/image_compressor_cli --input /path/to/input/folder --output /path/to/output/folder --quality 85 --max-dimension 1920 ``` -------------------------------- ### Build the Image Compressor CLI Source: https://github.com/starc007/image-compression/blob/main/readme.md Commands to clone the repository and compile the project from source using Cargo. ```bash git clone https://github.com/starc007/Image-compression.git cd Image-compression ``` ```bash cargo build --release ``` -------------------------------- ### Execute Image Compressor CLI commands Source: https://context7.com/starc007/image-compression/llms.txt Run the tool from the terminal with various flags for input, output, quality, and dimension settings. ```bash # Basic usage - compress images from current directory ./image_compressor_cli # Specify input and output directories ./image_compressor_cli --input /path/to/photos --output /path/to/compressed # Full options with custom quality and max dimension ./image_compressor_cli \ --input ./my_images \ --output ./compressed_output \ --quality 80 \ --max-dimension 1920 # Short form arguments ./image_compressor_cli -i ./source -o ./destination -q 75 # View help ./image_compressor_cli --help # Output: # Compresses images in a folder # # Usage: image_compressor_cli [OPTIONS] # # Options: # -i, --input Input directory path [default: .] # -o, --output Output directory path [default: compressed_images] # -q, --quality Compression quality (0-100) [default: 85] # --max-dimension Maximum image dimension [default: 1280] # -h, --help Print help # -V, --version Print version ``` -------------------------------- ### Process directories with process_directory Source: https://context7.com/starc007/image-compression/llms.txt Invoke the directory processing function to compress images while preserving the folder structure. ```rust use std::path::Path; // Process a directory with default quality and dimension settings let input_dir = Path::new("./photos"); let output_dir = Path::new("./compressed"); let quality = 85u8; let max_dimension = 1920u32; match compressor::process_directory(input_dir, output_dir, quality, max_dimension) { Ok(_) => println!("All images compressed successfully!"), Err(e) => eprintln!("Error during compression: {}", e), } // Example directory structure: // ./photos/ // ├── vacation/ // │ ├── beach.jpg // │ └── sunset.png // └── portrait.jpeg // // After processing, output structure mirrors input: // ./compressed/ // ├── vacation/ // │ ├── beach.jpg (compressed) // │ └── sunset.png (optimized with oxipng) // └── portrait.jpeg (compressed) ``` -------------------------------- ### Define CLI arguments with Opts struct Source: https://context7.com/starc007/image-compression/llms.txt Use the clap derive macro to define command-line arguments and parse them within the application. ```rust use clap::Parser; #[derive(Parser)] #[clap(version = "1.0", author = "Your Name", about = "Compresses images in a folder")] pub struct Opts { /// Input directory path #[clap(short, long, default_value = ".")] pub input: String, /// Output directory path #[clap(short, long, default_value = "compressed_images")] pub output: String, /// Compression quality (0-100) #[clap(short, long, default_value = "85")] pub quality: u8, /// Maximum image dimension #[clap(long, default_value = "1280")] pub max_dimension: u32, } // Parse arguments in your application let opts = cli::parse_args(); println!("Input: {}", opts.input); // Default: "." println!("Output: {}", opts.output); // Default: "compressed_images" println!("Quality: {}", opts.quality); // Default: 85 println!("Max Dimension: {}", opts.max_dimension); // Default: 1280 ``` -------------------------------- ### Compress Single Image Source: https://context7.com/starc007/image-compression/llms.txt Compresses an image by resizing and applying format-specific optimizations. JPEG uses quality, PNG uses oxipng, and others use native encoding. Requires `image` and `oxipng` crates. ```rust use std::path::Path; use image::{GenericImageView, ImageFormat, imageops::FilterType}; // Internal function signature fn compress_image( input_path: &Path, output_path: &Path, quality: u8, // 0-100, only affects JPEG max_dimension: u32 // Maximum width or height ) -> Result<(), Box> // Example compression workflow (internal implementation): let img = image::open(input_path)?; let (width, height) = img.dimensions(); // Calculate new dimensions maintaining aspect ratio let (new_width, new_height) = utils::calculate_new_dimensions(width, height, max_dimension); // Resize using high-quality Lanczos3 filter let resized = img.resize(new_width, new_height, FilterType::Lanczos3); // Format-specific saving: // - JPEG: Applies quality setting (0-100) // - PNG: Saves then optimizes with oxipng // - Others: Native format encoding match output_format { ImageFormat::Jpeg => { resized.write_to(&mut output_file, image::ImageOutputFormat::Jpeg(quality))?; }, ImageFormat::Png => { resized.save(output_path)?; oxipng::optimize(&input, &output, &Options::default())?; }, _ => { resized.save_with_format(output_path, output_format)?; }, } ``` -------------------------------- ### Validate image formats with is_supported_format Source: https://context7.com/starc007/image-compression/llms.txt Verifies if a file extension is supported by the compressor. The function performs case-insensitive matching. ```rust use std::ffi::OsStr; use crate::utils::is_supported_format; // Supported formats return true assert!(is_supported_format(OsStr::new("jpg"))); assert!(is_supported_format(OsStr::new("jpeg"))); assert!(is_supported_format(OsStr::new("png"))); assert!(is_supported_format(OsStr::new("gif"))); assert!(is_supported_format(OsStr::new("webp"))); assert!(is_supported_format(OsStr::new("tiff"))); assert!(is_supported_format(OsStr::new("tga"))); assert!(is_supported_format(OsStr::new("bmp"))); assert!(is_supported_format(OsStr::new("ico"))); assert!(is_supported_format(OsStr::new("hdr"))); // Case-insensitive matching assert!(is_supported_format(OsStr::new("JPG"))); assert!(is_supported_format(OsStr::new("PNG"))); // Unsupported formats return false assert!(!is_supported_format(OsStr::new("pdf"))); assert!(!is_supported_format(OsStr::new("svg"))); assert!(!is_supported_format(OsStr::new("raw"))); assert!(!is_supported_format(OsStr::new("txt"))); ``` -------------------------------- ### compress_image Function Source: https://context7.com/starc007/image-compression/llms.txt Compresses a single image file by resizing it to fit within a maximum dimension and applying format-specific compression. ```APIDOC ## compress_image ### Description Compresses a single image file by resizing it to fit within the maximum dimension while maintaining aspect ratio, then applies format-specific compression. JPEG images use quality-based compression, PNG images are optimized losslessly with oxipng, and other formats are saved with their native encoding. ### Parameters - **input_path** (&Path) - Required - Path to the source image file. - **output_path** (&Path) - Required - Path where the compressed image will be saved. - **quality** (u8) - Required - Compression quality (0-100), only affects JPEG. - **max_dimension** (u32) - Required - Maximum width or height for the resized image. ``` -------------------------------- ### Determine Output Image Format Source: https://context7.com/starc007/image-compression/llms.txt Determines the `ImageFormat` enum variant from a file path's extension. Defaults to JPEG for unknown extensions. Supports common and several other image formats. ```rust use std::path::Path; use image::ImageFormat; use crate::utils::determine_output_format; // Standard formats let format = determine_output_format(Path::new("photo.jpg")); assert_eq!(format, ImageFormat::Jpeg); let format = determine_output_format(Path::new("image.jpeg")); assert_eq!(format, ImageFormat::Jpeg); let format = determine_output_format(Path::new("screenshot.png")); assert_eq!(format, ImageFormat::Png); let format = determine_output_format(Path::new("animation.gif")); assert_eq!(format, ImageFormat::Gif); let format = determine_output_format(Path::new("modern.webp")); assert_eq!(format, ImageFormat::WebP); // Other supported formats let format = determine_output_format(Path::new("scan.tiff")); // ImageFormat::Tiff let format = determine_output_format(Path::new("texture.tga")); // ImageFormat::Tga let format = determine_output_format(Path::new("icon.bmp")); // ImageFormat::Bmp let format = determine_output_format(Path::new("favicon.ico")); // ImageFormat::Ico let format = determine_output_format(Path::new("hdr_photo.hdr")); // ImageFormat::Hdr // Unknown extension defaults to JPEG let format = determine_output_format(Path::new("file.unknown")); assert_eq!(format, ImageFormat::Jpeg); ``` -------------------------------- ### Calculate New Image Dimensions Source: https://context7.com/starc007/image-compression/llms.txt Calculates new dimensions to fit within a maximum constraint while preserving aspect ratio. Handles landscape, portrait, square, and already-small images. ```rust use crate::utils::calculate_new_dimensions; // Landscape image (wider than tall) let (new_w, new_h) = calculate_new_dimensions(4000, 3000, 1920); // Result: (1920, 1440) - width limited to 1920, height scaled proportionally // Portrait image (taller than wide) let (new_w, new_h) = calculate_new_dimensions(3000, 4000, 1920); // Result: (1440, 1920) - height limited to 1920, width scaled proportionally // Square image let (new_w, new_h) = calculate_new_dimensions(3000, 3000, 1920); // Result: (1920, 1920) - both dimensions scaled equally // Image already smaller than max dimension let (new_w, new_h) = calculate_new_dimensions(800, 600, 1920); // Result: (800, 600) - no change needed, dimensions preserved ``` -------------------------------- ### determine_output_format Function Source: https://context7.com/starc007/image-compression/llms.txt Determines the appropriate ImageFormat enum variant based on the output file's extension. ```APIDOC ## determine_output_format ### Description Determines the appropriate ImageFormat enum variant based on the output file's extension. This ensures images are saved in their original format, defaulting to JPEG for unknown extensions. ### Parameters - **path** (&Path) - Required - The file path to extract the extension from. ``` -------------------------------- ### calculate_new_dimensions Function Source: https://context7.com/starc007/image-compression/llms.txt Calculates new image dimensions that fit within the maximum dimension constraint while preserving the original aspect ratio. ```APIDOC ## calculate_new_dimensions ### Description Calculates new image dimensions that fit within the maximum dimension constraint while preserving the original aspect ratio. The function determines whether width or height is the limiting factor and scales accordingly. ### Parameters - **width** (u32) - Required - Original image width. - **height** (u32) - Required - Original image height. - **max_dimension** (u32) - Required - The maximum allowed dimension for either width or height. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.