所以,我对 Grunt 还比较陌生。
我一直在玩 grunt-contrib-imagemin。
我注意到在压缩 PNG 时,它做得很好。它平均压缩购买量 80%。
但是 jpeg 压缩几乎毫无意义。它通常只能从 3mb 的图片中压缩几 KB。
如何更好地压缩 jpeg?
Gruntfile.js
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
build: {
src: 'src/*.js',
dest: 'build/main.min.js'
}
},
imagemin: {
png: {
options: {
optimizationLevel: 7
},
files: [
{
expand: true,
cwd: 'src/img/png/', // cwd is 'current working directory'
src: ['**/*.png'],
dest: 'build/img/png', // Could also match cwd.
ext: '.png'
}
]
},
jpg: {
options: {
progressive: true
},
files: [
{
expand: true, // Tell Grunt where to find our images and where to export them to.
cwd: 'src/img/', // cwd is 'current working directory'
src: ['**/*.jpg'],
dest: 'build/img', // Could also match cwd.
ext: '.jpg'
}
]
}
}
});
// Load the plugins
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-imagemin');
// Default tasks
grunt.registerTask('default', ['uglify', 'imagemin']);
};
命令行界面
C:\Users\bmildren\Desktop\Sites\grunt-test>grunt
Running "uglify:build" (uglify) task
>> 1 file created.
Running "imagemin:png" (imagemin) task
? src/img/png/e4454c8df9.png (saved 1.37 MB)
Minified 1 image (saved 1.37 MB)
Running "imagemin:jpg" (imagemin) task
? src/img/5f468e98.jpg (saved 3.24 kB)
? src/img/photo-1414604582943-2fd913b3cb17.jpg (saved 3.24 kB)
? src/img/photo-1416424500327-a57ace7358b8.jpg (saved 3.24 kB)
? src/img/photo-1416838375725-e834a83f62b7.jpg (saved 3.24 kB)
? src/img/photo-1419332563740-42322047ff09.jpg (saved 3.24 kB)
Minified 5 images (saved 16.20 kB)
Done, without errors.
C:\Users\bmildren\Desktop\Sites\grunt-test>
最佳答案
grunt-contrib-imagemin 使用 jpegtran
来优化 jpg 文件。
它使用选项-copy none -optimize
从文件中剥离元数据并优化Huffman table这是一个无损的过程。
jpegtran
不执行有损操作,例如更改图像质量。因此,要回答您的问题,您不能使用 grunt-contrib-imagemin 进一步优化 jpg 文件,您需要使用其他东西。
png 文件使用 optipng
进行优化,它能够应用不同的算法,这些算法似乎对该文件类型更有效,同时保持无损。
关于javascript - 为什么 grunt-contrib-imagemin 将 PNG 减少 80%,但 JPG 仅减少 <0.1%?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28093591/