This tool allows you to optimize images by converting them to different formats, adjusting quality, and optionally cropping them. It supports batch processing of images in a directory and can also download and process images from URLs.
- Convert images to WebP or PNG format.
- Adjust image quality.
- Crop images to specified dimensions.
- Batch process images in a directory.
- Download and process images from URLs.
- Display optimization statistics.
To use this tool, you need to have Node.js installed. Then, install the required dependencies:
npm install fs node-fetch ora path readline sharp
Run the script using yarn/ npm:
yarn run optimize or npm run optimize
Follow the prompts to provide the necessary inputs:
- Enter path to image/image folder (or URL): Provide the path to a single image, a directory containing images, or a URL to download an image.
- Choose Image Format: Select the output format (WebP or PNG).
- Enter quality (0-100, default is 80): Specify the quality of the output image.
- Do you want to crop the image? (y/n): Choose whether to crop the image.
- Enter width and height separated by space (e.g., "800 600"): If cropping, provide the dimensions.
yarn run optimize or npm run optimize
Example interaction:
1. Enter path to image/image folder (or URL): /path/to/image.jpg
2. Choose Image Format:
1. webp
2. png
Enter your choice (1 or 2): 1
3. Enter quality (0-100, default is 80): 80
4. Do you want to crop the image? (y/n): y
Enter width and height separated by space (e.g., "800 600"): 800 600
The optimized images will be saved in an optimized
directory within the same directory as the input images. The script will display optimization statistics, including the original and optimized sizes, formats, dimensions, and the percentage of space saved.
This project is licensed under the MIT License.