Compression-CLI is a simple command-line utility built using Node.js that compresses PNG images, similar to TinyPNG. It allows you to reduce the size of PNG files while maintaining acceptable image quality. This utility uses the sharp library to perform image compression, and yargs to handle command-line arguments.
- Compress PNG images to reduce file size.
- Specify output quality and compression level.
- Resize images to a specific width while retaining aspect ratio.
- Automatic output file naming.
- Easy-to-use command-line interface.
- Node.js (version 14 or higher recommended)
- npm (Node Package Manager)
- Clone the repository or create your project directory:
git clone <repository-url> cd compression-cli
- Install the required dependencies:
npm install
To compress an image, use the following command:
node compress.js --input-file <path-to-input-file> [options]--input-fileor-i(required): The path to the PNG file you want to compress.--output-fileor-o(optional): The path where the compressed file should be saved. If not specified, the output file will have the same name as the input file, with-compressedappended before the extension.--qualityor-q(optional): The quality of the compressed image (0-100). Default is70.--compressionLevelor-c(optional): The compression level for the PNG file (0-9). Default is9.--resize-widthor-r(optional): The width to resize the image to, while retaining the aspect ratio. The output file name will also include the new dimensions (e.g.,filename-compressed-<newWidth>x<newHeight>.png).
-
Basic Compression with Default Settings
node compress.js --input-file ./images/sample.png
This command compresses
sample.pngand saves it assample-compressed.pngin the same directory, using the default quality of70and compression level of9. -
Specify Output File Path
node compress.js --input-file ./images/sample.png --output-file ./compressed/output.png
This command compresses
sample.pngand saves it to./compressed/output.png. -
Set Custom Quality and Compression Level
node compress.js --input-file ./images/sample.png --quality 50 --compressionLevel 5
This command compresses the image with a quality of
50and a compression level of5, saving it assample-compressed.png. -
Resize Image to Specific Width
node compress.js --input-file ./images/sample.png --resize-width 500
This command resizes
sample.pngto a width of500pixels while retaining the aspect ratio, and saves the compressed image assample-compressed-<newWidth>x<newHeight>.png.
- The script logs the following information:
- Input file size in KB.
- Output file size in KB.
- Size saved in KB and percentage reduction.
Input file size: 1024.56 KB
Output file size: 512.78 KB
Size saved: 511.78 KB (49.97%)
Compressed image saved to: ./images/sample-compressed.png
This project is licensed under the MIT License.
Feel free to submit pull requests or issues if you'd like to contribute to the project.
- [Your Name]
- Thanks to the creators of the
sharplibrary for making image processing easy in Node.js. - Inspired by the TinyPNG service.