Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
The imagemin npm package is a powerful tool for image compression and optimization. It allows developers to reduce the size of images without losing quality, which can significantly improve the performance of web applications by reducing load times.
Basic Image Compression
This code demonstrates how to use imagemin to compress JPEG and PNG images. The images are taken from the 'images' directory and the compressed versions are saved in the 'build/images' directory. The quality of JPEG images is set to 75, and the quality of PNG images is set between 60% and 80%.
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminPngquant = require('imagemin-pngquant');
(async () => {
const files = await imagemin(['images/*.{jpg,png}'], {
destination: 'build/images',
plugins: [
imageminMozjpeg({quality: 75}),
imageminPngquant({quality: [0.6, 0.8]})
]
});
console.log(files);
})();
GIF Optimization
This code demonstrates how to optimize GIF images using imagemin. The GIF images are taken from the 'images' directory and the optimized versions are saved in the 'build/images' directory. The optimization level is set to 3, which is the highest level of optimization.
const imagemin = require('imagemin');
const imageminGifsicle = require('imagemin-gifsicle');
(async () => {
const files = await imagemin(['images/*.gif'], {
destination: 'build/images',
plugins: [
imageminGifsicle({optimizationLevel: 3})
]
});
console.log(files);
})();
SVG Optimization
This code demonstrates how to optimize SVG images using imagemin. The SVG images are taken from the 'images' directory and the optimized versions are saved in the 'build/images' directory. The SVGO plugin is configured to keep the viewBox attribute and not to remove IDs.
const imagemin = require('imagemin');
const imageminSvgo = require('imagemin-svgo');
(async () => {
const files = await imagemin(['images/*.svg'], {
destination: 'build/images',
plugins: [
imageminSvgo({
plugins: [
{removeViewBox: false},
{cleanupIDs: false}
]
})
]
});
console.log(files);
})();
Sharp is a high-performance image processing library that supports various image formats. It is known for its speed and efficiency, making it a popular choice for image resizing, cropping, and format conversion. Unlike imagemin, which focuses on compression and optimization, sharp provides a broader range of image manipulation functionalities.
Jimp is a pure JavaScript image processing library that does not require any external dependencies. It supports a wide range of image manipulation features, including resizing, cropping, and color adjustments. While imagemin is primarily used for image compression, Jimp offers a more comprehensive set of image editing tools.
Image-webpack-loader is a loader for webpack that optimizes images during the build process. It uses imagemin under the hood but integrates seamlessly with webpack, making it a convenient choice for developers who are already using webpack for their build process. It provides similar functionalities to imagemin but is designed to work specifically within the webpack ecosystem.
Minify images seamlessly
Sindre Sorhus' open source work is supported by the community
Special thanks to:npm install imagemin
import imagemin from 'imagemin';
import imageminJpegtran from 'imagemin-jpegtran';
import imageminPngquant from 'imagemin-pngquant';
const files = await imagemin(['images/*.{jpg,png}'], {
destination: 'build/images',
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
console.log(files);
//=> [{data: <Uint8Array 89 50 4e …>, destinationPath: 'build/images/foo.jpg'}, …]
Returns Promise<object[]>
in the format {data: Uint8Array, sourcePath: string, destinationPath: string}
.
Type: string[]
File paths or glob patterns.
Type: object
Type: string
Set the destination folder to where your files will be written. If no destination is specified, no files will be written.
Type: Array
The plugins to use.
Type: boolean
Default: true
Enable globbing when matching file paths.
Returns Promise<Uint8Array>
.
Type: Uint8Array
The image data to optimize.
Type: object
Type: Array
Plugins to use.
FAQs
Minify images seamlessly
We found that imagemin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.