gulp 压缩图片
压缩 图片文件可降低文件大小,提高图片加载速度。
找到规律转换为 gulp 代码
规律
找到 images/ 目录下的所有文件,压缩它们,将压缩后的文件存放在 dist/images/ 目录下。
gulp 代码
一、安装 gulp-imagemin 模块
提示:你需要使用命令行的 cd 切换至对应目录再进行安装操作和 gulp 启动操作。
在命令行输入 npm install gulp-imagemin
安装成功后你会看到如下信息:(安装时间可能会比较长取决于网络)
npm install gulp-imagemin
npm WARN deprecated [email protected]: cross-spawn no longer requires a build toolchain, use it instead!
> [email protected] postinstall D:\wamp\www\BootsDataTable\node_modules\gifsicle
> node lib/install.js
√ gifsicle pre-build test passed successfully
> [email protected] postinstall D:\wamp\www\BootsDataTable\node_modules\jpegtran-bin
> node lib/install.js
√ jpegtran pre-build test passed successfully
> [email protected] postinstall D:\wamp\www\BootsDataTable\node_modules\optipng-bin
> node lib/install.js
√ optipng pre-build test passed successfully
[email protected] D:\wamp\www\BootsDataTable
`-- [email protected]
+-- [email protected]
| +-- [email protected]
| +-- [email protected]
| | +-- [email protected]
| | +-- [email protected]
| | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | `-- [email protected]
| | `-- [email protected]
| `-- [email protected]
+-- [email protected]
| +-- [email protected]
| | +-- [email protected]
| | | +-- [email protected]
| | | | `-- [email protected]
| | | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | `-- [email protected]
| | `-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | | +-- [email protected]
| | | | | | | `-- [email protected]
| | | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | +-- [email protected]
| | | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | | `-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | | `-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | | `-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | | `-- [email protected]
| | | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | `-- [email protected]
| | | | `-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | | `-- [email protected]
| | | | `-- [email protected]
| | | `-- [email protected]
| | | `-- [email protected]
| | +-- [email protected]
| | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | | `-- [email protected]
| | | | `-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | `-- [email protected]
| | `-- [email protected]
| | +-- [email protected]
| | | `-- [email protected]
| | `-- [email protected]
| | +-- [email protected]
| | `-- [email protected]
| | `-- [email protected]
| `-- [email protected]
+-- [email protected]
| +-- [email protected]
| `-- [email protected]
+-- [email protected]
| +-- [email protected]
| `-- [email protected]
+-- [email protected]
| +-- [email protected]
| | `-- [email protected]
| `-- [email protected]
| +-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| +-- [email protected]
| | `-- [email protected]
| +-- js-y[email protected]
| | +-- [email protected]
| | | `-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| `-- [email protected]
+-- [email protected]
| `-- [email protected]
+-- [email protected]
`-- [email protected]
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
二、创建 gulpfile.js 文件编写代码
在对应目录创建 gulpfile.js 文件并写入如下内容:
// 获取 gulp
var gulp = require(‘gulp‘);
// 获取 gulp-imagemin 模块
var imagemin = require(‘gulp-imagemin‘)
// 压缩图片任务
// 在命令行输入 gulp images 启动此任务
gulp.task(‘images‘, function () {
// 1. 找到图片
gulp.src(‘images/*.*‘)
// 2. 压缩图片
.pipe(imagemin({
progressive: true
}))
// 3. 另存图片
.pipe(gulp.dest(‘dist/images‘))
});
// 在命令行使用 gulp auto 启动此任务
gulp.task(‘auto‘, function () {
// 监听文件修改,当文件被修改则执行 images 任务
gulp.watch(‘images/*.*)‘, [‘images‘])
});
// 使用 gulp.task(‘default‘) 定义默认任务
// 在命令行使用 gulp 启动 images 任务和 auto 任务
gulp.task(‘default‘, [‘images‘, ‘auto‘])
你可以访问 gulp-imagemin 以查看更多用法。
三、在 images/ 目录下存放图片
在 gulpfile.js 对应目录创建 images 文件夹,并在 images/ 目录下存放图片。
你可以访问 https://github.com/nimojs/gulp-book/tree/master/demo/chapter4/images/ 下载示例图片
四、运行 gulp 查看效果
在命令行输入 gulp +回车
你将看到命令行出现如下提示
gulp
[11:07:37] Using gulpfile D:\wamp\www\BootsDataTable\gulpfile.js
[11:07:37] Starting ‘images‘...
[11:07:37] Finished ‘images‘ after 3.57 ms
[11:07:37] Starting ‘auto‘...
[11:07:37] Finished ‘auto‘ after 57 ms
[11:07:37] Starting ‘default‘...
[11:07:37] Finished ‘default‘ after 16 μs
[11:07:38] gulp-imagemin: Minified 20 images (saved 12.48 kB - 24.2%)