
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
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
$ npm install --save imagemin
var Imagemin = require('imagemin');
new Imagemin()
.src('images/*.{gif,jpg,png,svg}')
.dest('build/images')
.use(Imagemin.jpegtran({progressive: true}))
.run(function (err, files) {
console.log(files[0]);
// => {path: 'build/images/foo.jpg', contents: <Buffer 89 50 4e ...>}
});
You can use gulp-rename to rename your files:
var Imagemin = require('imagemin');
var rename = require('gulp-rename');
new Imagemin()
.src('images/foo.png')
.use(rename('bar.png'));
Creates a new Imagemin
instance.
Type: array
, buffer
or string
Set the files to be optimized. Takes a buffer, glob string or an array of glob strings as argument.
Type: string
Set the destination folder to where your files will be written. If you don't set any destination no files will be written.
Type: function
Add a plugin
to the middleware stack.
Type: function
Optimize your files with the given settings.
The callback will return an array of vinyl files in files
.
The following plugins are bundled with imagemin:
Compress GIF images.
var Imagemin = require('imagemin');
new Imagemin()
.use(Imagemin.gifsicle({interlaced: true}));
Compress JPG images.
var Imagemin = require('imagemin');
new Imagemin()
.use(Imagemin.jpegtran({progressive: true}));
Lossless compression of PNG images.
var Imagemin = require('imagemin');
new Imagemin()
.use(Imagemin.optipng({optimizationLevel: 3}));
Compress SVG images.
var Imagemin = require('imagemin');
new Imagemin()
.use(Imagemin.svgo());
$ npm install --global imagemin
$ imagemin --help
Usage
$ imagemin <file> <directory>
$ imagemin <directory> <output>
$ imagemin <file> > <output>
$ cat <file> | imagemin > <output>
Example
$ imagemin images/* build
$ imagemin images build
$ imagemin foo.png > foo-optimized.png
$ cat foo.png | imagemin > foo-optimized.png
Options
-i, --interlaced Interlace gif for progressive rendering
-o, --optimizationLevel <number> Optimization level between 0 and 7
-p, --progressive Lossless conversion to progressive
MIT © imagemin
FAQs
Minify images seamlessly
The npm package imagemin receives a total of 791,095 weekly downloads. As such, imagemin popularity was classified as popular.
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 1 open source maintainer 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.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.