Skip to content

ssi02014/image-webp-converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

30 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒ„ Image WebP Converter

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!


Why Image Webp Converter?

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, and PNG
  • Faster website loading times
  • Supported by all modern browsers
  • Perfect for web optimization

๐Ÿ“ฆ Installation

Since this is a CLI tool, install the package as a devDependency using one of the following commands:

yarn add -D image-webp-converter
pnpm add -D image-webp-converter
npm install --save-dev image-webp-converter

๐Ÿ“ Usage

After 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 ./images directory of the current directory. Therefore, if the ./images directory does not exist, you need to create it and add images.

By default, converted images are saved in the ./images/webp directory. This directory will be automatically created if it doesn't exist.

Supported image formats are: JPG, JPEG, and PNG. These formats will be converted to WebP format during the conversion process.


Run the following command to convert the images to WebP format.

yarn webpc
pnpm run webpc
npm run webpc

Result

your-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.

Image

๐Ÿ”ง Options

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 ...

1. Path

--p or --path: The path to the images to convert.

  • default: ./images
yarn webpc --p ./images
yarn webpc --path ./images
pnpm run webpc --p ./images
pnpm run webpc --path ./images
npm run webpc --p ./images
npm run webpc --path ./images

2. Destination

--d or --destination: The path to save the converted images.

  • default: ./images/webp
yarn webpc --d ./images/webp
yarn webpc --destination ./images/webp
pnpm run webpc --d ./images/webp
pnpm run webpc --destination ./images/webp
npm run webpc --d ./images/webp
npm run webpc --destination ./images/webp

3. Quality

--q or --quality: The quality of the converted images.

  • default: 75
yarn webpc --q 90
yarn webpc --quality 90
pnpm run webpc --q 90
pnpm run webpc --quality 90
npm run webpc --q 90
npm run webpc --quality 90

4. Lossless

--l or --lossless: Use lossless compression.

  • default: false
yarn webpc --l true
yarn webpc --lossless true
pnpm run webpc --l true
pnpm run webpc --lossless true
npm run webpc --l true
npm run webpc --lossless true

5. Size

--s or --size: Limit the output file size to the specified byte size.

yarn webpc --s 100000
yarn webpc --size 100000
pnpm run webpc --s 100000
pnpm run webpc --size 100000
npm run webpc --s 100000
npm run webpc --size 100000

6. Resize

--r or --resize: Resize the images by specifying width and height.

  • width: Width of the resized image (pixels) - required
  • height: Height of the resized image (pixels) - required
yarn webpc --r.width 100 --r.height 100
yarn webpc --resize.width 100 --resize.height 100
pnpm run webpc --r.width 100 --r.height 100
pnpm run webpc --resize.width 100 --resize.height 100
npm run webpc --r.width 100 --r.height 100
npm run webpc --resize.width 100 --resize.height 100

7. Crop

--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) - required
  • y: Starting y-coordinate for cropping (pixels) - required
  • width: Width of the crop area (pixels) - required
  • height: 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 100
pnpm 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 100
npm 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 100

๐Ÿ“ Configuration

You can also use a configuration file to customize the conversion process.

create a webpc.config.mjs file in the root of your project.

  • cjs config 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.


๐Ÿค Acknowledgements

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.


๐Ÿ“ License

MIT ยฉ ssi02014. See LICENSE for details.


About

๐ŸŒ„ A powerful yet simple CLI tool that converts your images to WebP format

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published