简体   繁体   中英

Why doesn grunt-contrib-imagemin reduce PNGs by 80% but JPG by only <0.1%?

So, I'm relatively new to Grunt.

I've been playing around with grunt-contrib-imagemin.

I have noticed that when compressing PNGs, it does a great job. It averagely compresses buy 80%.

But the jpeg compression is almost pointless. It often compresses just a couple of KB from a 3mb picture.

How do I make it better a compressing jpegs?

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.

It uses the options -copy none -optimize which strips meta-data from the file and optimizes the Huffman table which is a lossless process.

jpegtran does not perform lossy operations such as changing the image quality. So to answer your question, you can not use grunt-contrib-imagemin to optimize jpg files further, you would need to use something else.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM