繁体   English   中英

grunt-contrib-imagemin - 拍摄两个不同文件夹的图像

[英]grunt-contrib-imagemin - Take images of two different folders

我试图设置grunt-imagemin拍摄两个不同文件夹的图像。

我有两个文件夹:

  1. 图片 - 用户
  2. 图片 - 产品。

我的想法是使用grunt-imagemin和grunt-watch然后避免手动执行此任务。 我有一个拥有大量流量的网站,当我手动执行此操作时,CPU会崩溃。 我认为在用户上传图片时这样做可能会更好。

我的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']);

谢谢。

我想将答案分为两部分。

第一部分:我尝试使用grunt-watch,grunt-newer和grunt-imagemin,但我无法使其工作。 当有人上传图像时,grunt-watch会在图像结束上传到服务器之前检测到此事件。 所以,grunt-imagemin失败了。

为了解决这个问题,我使用了gm包,但是如果你使用另一种语言,我确信有一个类似的库。

第二部分:如果你来这篇文章看不同文件夹的照片。 我很容易解决这个问题。

您尝试使用此代码:

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/"

        }]
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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