Last Updated: February 25, 2016
·
2.905K
· grayghostvisuals

Optimize .jpg and .png w/Grunt

The best setup for optimizing .png and .jpg with grunt-contrib-imagemin.

grunt.initConfig({
  imagemin: {
    png: {
      options: {
        optimizationLevel: 7
      },
      files: [
        {
          expand: true,
          cwd: 'project-directory/img/', // cwd is 'current working directory'
          src: ['**/*.png'],
          dest: 'project-directory/img/compressed/', // 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: 'project-directory/img/', // cwd is 'current working directory'
          src: ['**/*.jpg'],
          dest: 'project-directory/img/compressed/', // Could also match cwd.
          ext: '.jpg'
        }
      ]
    }
  }
});

grunt.registerTask('default', ['imagemin']);