[英]Why doesn grunt-contrib-imagemin reduce PNGs by 80% but JPG by only <0.1%?
So, I'm relatively new to Grunt. 因此,我对Grunt还是比较陌生。
I've been playing around with grunt-contrib-imagemin. 我一直在玩grunt-contrib-imagemin。
I have noticed that when compressing PNGs, it does a great job. 我注意到压缩PNG时做得很好。 It averagely compresses buy 80%.
它平均压缩购买80%。
But the jpeg compression is almost pointless. 但是jpeg压缩几乎没有意义。 It often compresses just a couple of KB from a 3mb picture.
它通常仅从3mb的图片中压缩几个KB。
How do I make it better a compressing jpegs? 如何更好地压缩jpeg?
Gruntfile.js 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']);
};
CLI 命令行界面
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 uses jpegtran
to optimize jpg files. grunt-contrib-imagemin使用
jpegtran
优化jpg文件。
It uses the options -copy none -optimize
which strips meta-data from the file and optimizes the Huffman table which is a lossless process. 它使用选项
-copy none -optimize
从文件中剥离元数据并优化Huffman表 ,这是一个无损过程。
jpegtran
does not perform lossy operations such as changing the image quality. jpegtran
不会执行有损操作,例如更改图像质量。 So to answer your question, you can not use grunt-contrib-imagemin to optimize jpg files further, you would need to use something else. 因此,要回答您的问题,您不能使用grunt-contrib-imagemin进一步优化jpg文件,而需要使用其他方法。
png files are optimized with optipng
which is able to apply different algorithms which appear to be more effective for that file type while remaining lossless. png文件已使用
optipng
进行了优化,该optipng
可以应用不同的算法,这些算法对于该文件类型似乎更有效,同时保持无损。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.