Picture compression of Gulp tutorial

Source: Internet
Author: User

Gulp.js is an automated building tool, GULP configuration is very simple, you can use GULP to achieve a lot of functions, such as gulp-imagemin with the implementation of image compression. The following PHP programmer Rechesson A detailed description of using Gulp-imagemin compressed image files (including PNG, JPEG, GIF, and SVG pictures).

1., Global Installation Gulp

1
$ NPM Install--global Gulp

2. Local Installation Gulp-imagemin

1
$ NPM Install gulp-imagemin--save-dev

3. Configure Gulpfile.js Tasks (Task)
A, basic usage
Compress the picture in the Src/img directory to the DIST/IMG directory

1
2
3
4
5
6
7
8
9
10
11
12
13
var gulp = require (' Gulp '),
Imagemin = require (' gulp-imagemin ');

Gulp. Task (' Imagemin ', function () {
Gulp. SRC (' src/img/*.{ Png,jpg,gif,ico} ')
. Pipe (Imagemin ({
Optimizationlevel:5,//Type: Number default: 5 Value range: 0-7 (Optimization level)
Progressive:true,//Type: Boolean default: False lossless compression jpg picture
Interlaced:true,//Type: Boolean default: False interlaced GIF for rendering
Multipass:true//Type: Boolean default: False multiple optimizations to optimize SVG until fully optimized
} ) )
. Pipe (Gulp dest (' dist/img '));
} ) ;

b, use imagemin-pngquant depth compression picture

1
$ NPM Install imagemin-pngquant--save-dev
1
2
3
4
5
6
7
8
9
10
11
var gulp = require (' Gulp '),
Imagemin = require (' Gulp-imagemin '),
Pngquant = require (' imagemin-pngquant ');

Gulp. Task (' Imagemin ', function () {
Gulp. SRC (' src/img/*.{ Png,jpg,gif,ico} ')
. Pipe (Imagemin ({
Use: [Pngquant ()]//Imagemin plugin with pngquant deep compression png picture
} ) )
. Pipe (Gulp dest (' dist/img '));
} ) ;

5, Gulp Execution compressed picture task

1
$ Gulp Imagemin
Tags: Gulp tutorials, Nodejs learning
Source: PHP Programmer Rechesson's Blog-Gulp Tutorial picture compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.