English | ํ๊ตญ์ด | ็ฎไฝไธญๆ | ๆฅๆฌ่ช
๐ A powerful yet simple CLI tool that converts your images to WebP format - the modern image format that provides superior compression for images on the web!
This tool simplifies the process of converting your JPG, JPEG, and PNG images to WebP format, helping you optimize your web assets with minimal effort. Here's what you can do:
- Convert multiple images at once
- Maintain image quality while reducing file size
- Customize output settings like quality and lossless
- Resize images during conversion
- Crop images to specific dimensions
- Process images in nested subdirectories
- Limit output file size to specific byte size
๐ก Benefits of WebP:
- Smaller file sizes compared to
JPG,JPEG, andPNG - Faster website loading times
- Supported by all modern browsers
- Perfect for web optimization
Since this is a CLI tool, install the package as a devDependency using one of the following commands:
yarn add -D image-webp-converterpnpm add -D image-webp-converternpm install --save-dev image-webp-converterAfter installing the package, add the following script to your package.json file.
// package.json
{
// ...
"scripts": {
"webpc": "webpc"
// ...
}
}Place your images in your desired directory (default: ./images):
your-project/
โโโ images/ # Directory for original images to convert(default: ./images)
โ โโโ image1.jpg
โ โโโ image2.png
โ โโโ image3.jpeg
โ โโโ nested/
โ โโโ image4.jpg
โ โโโ image5.png
โ โโโ image6.jpeg
โโโ src/
โโโ package.json
โโโ ...
๐ก Notes
By default, it converts all images (including those in nested subdirectories) in the
./imagesdirectory of the current directory. Therefore, if the./imagesdirectory does not exist, you need to create it and add images.
By default, converted images are saved in the
./images/webpdirectory. This directory will be automatically created if it doesn't exist.
Supported image formats are:
JPG,JPEG, andPNG. These formats will be converted to WebP format during the conversion process.
Run the following command to convert the images to WebP format.
yarn webpcpnpm run webpcnpm run webpcyour-project/
โโโ images/
โ โโโ image1.jpg
โ โโโ image2.png
โ โโโ image3.jpeg
โ โโโ nested/
โ โ โโโ image4.jpg
โ โ โโโ image5.png
โ โ โโโ image6.jpeg
โ โโโ webp/ # Directory for converted images(default: ./images/webp)
โ โโโ image1.webp
โ โโโ image2.webp
โ โโโ image3.webp
โ โโโ image4.webp
โ โโโ image5.webp
โ โโโ image6.webp
โโโ src/
โโโ package.json
โโโ ...
When the conversion is performed, you can check the conversion information as shown below.
The following options are available: path, destination, quality, lossless, resize, and crop. These options allow you to customize the conversion process according to your needs.
yarn webpc --path ./my-images --destination ./my-images/webp --quality 90 ...pnpm run webpc --path ./my-images --destination ./my-images/webp --quality 90 ...npm run webpc --path ./my-images --destination ./my-images/webp --quality 90 ...--p or --path: The path to the images to convert.
- default:
./images
yarn webpc --p ./images
yarn webpc --path ./imagespnpm run webpc --p ./images
pnpm run webpc --path ./imagesnpm run webpc --p ./images
npm run webpc --path ./images--d or --destination: The path to save the converted images.
- default:
./images/webp
yarn webpc --d ./images/webp
yarn webpc --destination ./images/webppnpm run webpc --d ./images/webp
pnpm run webpc --destination ./images/webpnpm run webpc --d ./images/webp
npm run webpc --destination ./images/webp--q or --quality: The quality of the converted images.
- default:
75
yarn webpc --q 90
yarn webpc --quality 90pnpm run webpc --q 90
pnpm run webpc --quality 90npm run webpc --q 90
npm run webpc --quality 90--l or --lossless: Use lossless compression.
- default:
false
yarn webpc --l true
yarn webpc --lossless truepnpm run webpc --l true
pnpm run webpc --lossless truenpm run webpc --l true
npm run webpc --lossless true--s or --size: Limit the output file size to the specified byte size.
yarn webpc --s 100000
yarn webpc --size 100000pnpm run webpc --s 100000
pnpm run webpc --size 100000npm run webpc --s 100000
npm run webpc --size 100000--r or --resize: Resize the images by specifying width and height.
width: Width of the resized image (pixels) -requiredheight: Height of the resized image (pixels) -required
yarn webpc --r.width 100 --r.height 100
yarn webpc --resize.width 100 --resize.height 100pnpm run webpc --r.width 100 --r.height 100
pnpm run webpc --resize.width 100 --resize.height 100npm run webpc --r.width 100 --r.height 100
npm run webpc --resize.width 100 --resize.height 100--c or --crop: Options for cropping images. Crops the image starting from the specified coordinates (x,y) with the set width and height.
x: Starting x-coordinate for cropping (pixels) -requiredy: Starting y-coordinate for cropping (pixels) -requiredwidth: Width of the crop area (pixels) -requiredheight: Height of the crop area (pixels) -required
yarn webpc --c.x 100 --c.y 100 --c.width 100 --c.height 100
yarn webpc --crop.x 100 --crop.y 100 --crop.width 100 --crop.height 100pnpm run webpc --c.x 100 --c.y 100 --c.width 100 --c.height 100
pnpm run webpc --crop.x 100 --crop.y 100 --crop.width 100 --crop.height 100npm run webpc --c.x 100 --c.y 100 --c.width 100 --c.height 100
npm run webpc --crop.x 100 --crop.y 100 --crop.width 100 --crop.height 100You can also use a configuration file to customize the conversion process.
create a webpc.config.mjs file in the root of your project.
cjsconfig file is not supported.
your-project/
โโโ images/
โโโ src/
โโโ package.json
โโโ webpc.config.mjs
โโโ ...
// webpc.config.mjs
export default {
path: "images",
destination: "images/webp",
quality: 80,
lossless: false,
// object: resize, crop
resize: {
width: 500, // required
height: 500, // required
},
// ...
};๐ก Notes
command-line interface (CLI) arguments take precedence over options defined in the configuration file.
This project was created with reference to the following projects. These libraries provide powerful image optimization and WebP conversion capabilities that form the foundation of this tool.
MIT ยฉ ssi02014. See LICENSE for details.