简体   繁体   English

为什么grunt-contrib-imagemin可以将PNG减少80%,将JPG减少<0.1%?

[英]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.

相关问题 为什么grunt-contrib-imagemin不使用我设置的选项? - Why is grunt-contrib-imagemin not using the option I set? grunt-contrib-imagemin将文件输出到错误的文件夹 - grunt-contrib-imagemin outputting files to the wrong folder 运行grunt-contrib-imagemin时出错 - Getting error when running grunt-contrib-imagemin 如何使用grunt-contrib-imagemin插件无损优化图像? - How to lossless optimize images with plugins for grunt-contrib-imagemin? grunt-contrib-imagemin - 拍摄两个不同文件夹的图像 - grunt-contrib-imagemin - Take images of two different folders 错误:使用 grunt-contrib-imagemin 时为 png 文件生成 ENOENT - Error: spawn ENOENT for png files when using grunt-contrib-imagemin 如果使用grunt-contrib-imagemin优化图像,如何在手柄模板中处理图像 - How to address images in handlebars templates if the images are optimized with grunt-contrib-imagemin travis-ci在grunt-contrib-imagemin任务上构建失败(无法准备未定义的属性“ contents” - travis-ci build failure on grunt-contrib-imagemin task (Cannot ready property 'contents' of undefined 为什么grunt-contrib-connect找不到连接任务? - Why doesn't grunt-contrib-connect find the connect task? 为什么使用grunt-contrib-cssmin会更改我的CSS,同时只能缩小 - why using grunt-contrib-cssmin is changing my css while it should only minify that
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM