javascript

Compress images using Gulp

//Installation - npm install --save-dev gulp-imagemin
const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
 
gulp.task('compress_img', function() {
    return gulp.src('src/images/*')
            .pipe(imagemin())
            .pipe(gulp.dest('dist/images'))
})

To compress images using gulp you can use gulp-imagemin npm package. The imagemin() function that is used to compress image will decrease the size of images.

NPM package installation

npm install --save-dev gulp-imagemin

You can also pass options that inside imagemin() function to make images compress as you want. The options can be:

imagemin({
    interlaced: true,
    progressive: true,
    optimizationLevel: 5,
    svgoPlugins: [
        {
            removeViewBox: true
        }
    ]
})
Was this helpful?