续Gulp使用入门三步压缩图片

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%)

时间: 2024-11-11 19:01:33

续Gulp使用入门三步压缩图片的相关文章

续Gulp使用入门三步压缩CSS

gulp 压缩css 一.安装 gulp-minify-css 模块 提示:你需要使用命令行的 cd 切换到对应目录后进行安装操作. 在命令行输入 npm install gulp-minify-css 安装成功后你会看到如下信息:(安装时间可能会比较长取决于网络) D:\wamp\www\BootsDataTable>npm install gulp-minify-cssnpm WARN deprecated [email protected]: Please use gulp-clean-c

续Gulp使用入门编译Sass

使用 gulp 编译 Sass Sass 是一种 CSS 的开发工具,提供了许多便利的写法,大大节省了开发者的时间,使得 CSS 的开发,变得简单和可维护. 安装 npm install gulp-sass (--save-dev) 括号中的可选 基本用法 Something like this will compile your Sass files: 'use strict'; var gulp = require('gulp');var sass = require('gulp-sass'

ar游戏开发入门三步走 - 转载请保留原文链接:http://www.chinaar.com/ZYJC/2423.html

中国AR网(公众号armeiti):其实我遇到很多像你这样的去做AR开发的.其实现在很多AR开发者根本不懂AR实现原理,因为现在开发AR不需要自己去做底层算法,直接去用成熟的SDK即可.现在AR应用开发门槛不是像以前那么高了,反而好的创意更多的来自内容 ar游戏开发入门一般需要 算法.应用开发.3D美工三部分: 你说的 opencv 主要用与算法部分. java,c# 属于应用开发部分. 国内一般只做后两部分,借助于 高通 或 美桃(Metaio) 的识别算法, 使用 Unity 作为开发平台,

Office操作:图文混排三步曲

我们在编辑Word文档的时候,图片和文字的混排编辑可能是我们遇到的最为主要的情况,恰到好处的图文混排不仅可以起到美化文档的效果,对于阅读者阅读并理解文档内容也是大有裨益.因此,熟练掌握Word的图文混排是我们制作比如海报.杂志.刊物.贺卡等这类非纯文本文档的第一步,今天,我在这给大家介绍图文混排的基本知识及技巧,先给大家展示一个效果图. Office操作:图文混排三步曲效果展示准备工作:1.准备好文字材料:2.准备好图片素材:3.构思好排版效果. 图文混排第一步:在文档中插入图片.方法:通过[插

使用 gulp 压缩图片

请务必理解如下章节后阅读此章节: 安装 Node 和 gulp 使用 gulp 压缩 JS 压缩 图片文件可降低文件大小,提高图片加载速度. 找到规律转换为 gulp 代码 规律 找到 images/ 目录下的所有文件,压缩它们,将压缩后的文件存放在 dist/images/ 目录下. gulp 代码 一.安装 gulp-imagemin 模块 提示:你需要使用命令行的 cd 切换至对应目录再进行安装操作和 gulp 启动操作. 在命令行输入 npm install gulp-imagemin

快速入门git第三步

安装:一.在linux ubuntu 上安装git sudo apt-get install git配置:二.初次运行git的配置,为什么是初次,因为更新或升级都会沿用老的git的配置, 可以用相同的命令进行了配置的修改, git 提供了一个工具git config ,专门用来配置或读取相应的环境变量,不同的环境变量导致了git在各个环节的具体工作方式和行为,环境变量存放在三个地方:/ect/config: 系统中对所有的用户都普遍使用的配置-/config: 只是适用与当前用户的配置.git/

[转]Membership三步曲之入门篇 - Membership基础示例

本文转自:http://www.cnblogs.com/jesse2013/p/membership.html Membership三步曲之入门篇 - Membership基础示例 Membership 三步曲之入门篇 - Membership基础示例 Membership三步曲之入门篇 -  Membership基础示例 Membership三步曲之进阶篇 -  深入剖析Provider model Membership三步曲之高级篇 -  从Membership 到 .NET 4.5 之 A

Android三步显示gif动态图片

今天给大家带来一个显示gif动态图片的类库,使用方法很简单,只需三步. 1.把GifView.jar加入你的项目. 2.在布局xml中这样写: <com.ant.liao.GifView android:id="@+id/gif1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:paddingTop="4px&quo

Gulp新手入门教程

Gulp 是一个自动化工具,前端开发者可以使用它来处理常见任务: 搭建web服务器 文件保存时自动重载浏览器 使用预处理器如Sass.LESS 优化资源,比如压缩CSS.JavaScript.压缩图片 当然Gulp能做的远不止这些.如果你够疯狂,你甚至可以使用它搭建一个静态页面生成器.Gulp真的足够强大,但你必须学会驾驭它. 这是这篇文章的主要目的.帮助你了解Gulp的基础用法,助你早日完成一统天下的大业. 在我们深入了解之前,我们先来说说为什么是Gulp. 为什么是Gulp? 类似Gulp的