不多说了直接上菜
项目下文件
package.json
{ "name": "DEMO", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "node index.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "gulp-imagemin": "^3.4.0", "imagemin-gifsicle": "^5.2.0", "imagemin-jpegtran": "^5.0.2", "imagemin-pngquant": "^5.0.1" }, "devDependencies": { } }
index.js
const path = require('path'); const imagemin = require('imagemin'); const imageminPngquant = require('imagemin-pngquant'); const imageminGifsicle = require('imagemin-gifsicle'); const imageminJpegtran = require('imagemin-jpegtran'); imagemin(['images/*.{png,jpg,gif}'], 'build/images', { plugins: [ imageminJpegtran(), imageminPngquant({quality: '50'}), imageminJpegtran(), imageminGifsicle({quality: '50'}) ]}).then(() => { console.log('Images optimized'); });
images
文件夹,放需要压缩的图片文件(jpg/png/gif)
README.md
- 把需要压缩的图片放到images文件夹下
- 运行
npm install
- 运行
npm run test
或者node index.js
- 执行成功后会在dist/images目录下生成压缩后的图片
项目下载地址
https://github.com/lydxwj/imageCompress
压缩图片网址
一大堆代码,搞得晕晕的,还不如直接用现成免费网站,太方便以至于怀疑人生,是否有必要捯饬上面的东东?
jpg/png/gif:iLoveIMG
jpg/png:TinyPNG