compress-images-with-sharp
v1.3.3
Published
Minify size your images. Image compression with extension: jpg/jpeg, png, webp
Downloads
51
Maintainers
Keywords
Readme
Compress Images with Sharp
This is a Node.js module that allows you to compress images in a directory using the Sharp image processing library. The module supports compressing images in the PNG, JPG, and JPEG formats.
Installation
To use this module, you need to have Node.js installed on your system. You can install it from the official website: https://nodejs.org/
To install the module, run the following command in your terminal:
npm install compress-images-with-sharp -D
Usage № 1
To use the module, you need to import it into your Node.js project and call the Compress
function, passing in the quality and directory parameters. The quality parameter is a number between 0 and 100 that specifies the quality of the compressed images. The directory parameter is a string that specifies the directory containing the images to be compressed.
Create a new file compress.js
in the root directory of your project and add the following code:
import { Compress } from 'compress-images-with-sharp';
Compress(80, 'static/assets');
or
import { Compress, COMPRESS_TYPE } from 'compress-images-with-sharp';
Compress(80, 'static/assets', COMPRESS_TYPE.FULL);
/**
* COMPRESS_TYPE includes ORIGINAL | FULL | WEBP formats
**/
Then add the script "compress": "node compress.js"
to the package.json
file so that you can run the compression from the command line:
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"compress": "node compress.js"
},
"dependencies": {
"compress-images-with-sharp": "^1.2.0"
}
}
Now you can now run compression from the command line by running the compression command:
npm run compress
Usage № 2
Using the example of image compression using npx
npx compress-images-with-sharp 100 static/assets webp
or
npx compress-images-with-sharp 100 static/assets original
or
npx compress-images-with-sharp 100 static/assets full
License
This module is licensed under the MIT License.