Grunt-contrib-imagemin - Capture images of two different folders

I am trying to establish that grunt-imagemin accepts images of two different folders.

I have two folders:

  • User images
  • Product images.

My idea is to use grunt-imagemin with grunt-watch and then not do this task manually. I have a site with a lot of traffic and when I do it manually the CPU crashes. I think it might be better to do this while users are uploading images.

My gruntfile.js:

grunt.initConfig({
    uglify: {
        files: { 
            src: 'client/js/views/*.js',  // source files mask
            dest: 'client/js/views/min/',    // destination folder
            expand: true,    // allow dynamic building
            flatten: true,   // remove all unnecessary nesting
            ext: '.js'   // replace .js to .min.js
        }
    },
    watch: {
        js:  { files: 'client/js/views/*.js', tasks: [ 'uglify' ] },
    },
    imagemin: {
        dist: {
            options: {
                optimizationLevel: 7,
                progressive: 5
            },
            files: [{
                expand: true,
                cwd: 'client/img/images-users',
                src: '**/*.{gif,GIF,jpg,JPG,png,PNG}',
                dest: 'client/img/images-users-compressed/'
            }]
        }
    }
});
// load plugins
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-newer');

// register at least this one task
grunt.registerTask('default', ['watch']);
grunt.registerTask('resize', ['newer:imagemin']);

      

Thank.

+3


source to share


1 answer


I would like to separate the answer in two parts.

First part: I tried with the gruff clock, grunt-newer and grunt-imagemin, but I couldn't get it to work. When someone uploads an image, grunt-watch detects this event before the image is uploaded to the server. So grunt-imagemin is failing.

To solve this I used the gm package , but if you are using a different language I am sure there is a similar library.



Second part: if you came to this post, look at the pictures of different folders. I solved this problem very easily.

You are trying this code:

imagemin: {
    dynamic: {
        options: {
            optimizationLevel: 7,
            progressive: true,
        },
        files: [{
            expand: true,
            cwd: "xx/img/your-path/",
            src: "**/*.{gif,GIF,jpg,JPG,png,PNG}",
            dest: "xx/img/your-path/compressed/"
        }, {
            expand: true,
            cwd: "xx/img/other-path/",
            src: "**/*.{gif,GIF,jpg,JPG,png,PNG}",
            dest: "xx/img/other-path/compressed/"

        }]
    }
}

      

+2


source







All Articles