Cannot optimize images inside folder using grunt imagemin

I am trying to optimize images using the grunt imagemin module .

My directory structure is as follows:

  • publicImg
    • Main
    • Loading
      • folder1
      • ...
      • folderN

Inside each of the folders 1..N there are files. All of them .jpg

What I want to achieve is to try to optimize them all and change them to new versions (with the same name). I started with a simpler task to optimize them and copy them to another folder ( publicImages ), keeping the same structure.

In the documentation, I came up with the following JSON snippet:

 imagemin: { dynamic: { files: [{ expand: true, cwd: 'publicImg/', src: ['*.jpg'], dest: 'publicImages/' }] } } 

which tells me Minified 0 images (saved 0 B) , I tried to use **/*jpg , **/*.{jpg} , but still with the same effect. How can this be done correctly?

+6
source share
1 answer

The following configuration works completely for me. Tested in the environment you described.

package.json

 { "devDependencies": { "grunt": "^0.4.5", "grunt-contrib-imagemin": "^0.7.1" } } 

Gruntfile.js

 module.exports = function (grunt) { grunt.initConfig({ imagemin: { dynamic: { files: [{ expand: true, cwd: 'publicImg/', src: ['**/*.jpg'], dest: 'publicImages/' }] } } }); grunt.loadNpmTasks('grunt-contrib-imagemin'); grunt.registerTask('default', ['imagemin']); }; 

Output

 raiden@toluca :~/projects/test$ tree publicImg/ publicImg/ ├── main └── upload ├── folder1 │  ├── 2499085-metal-gear-solid-5-exteded-e3.jpg │  └── url.htm ├── folder2 │  └── metal-gear-solid-5-ground-zeroes-collectibles.jpg └── folder3 ├── 2574364-8751006069-14019.jpg ├── Metal-Gear-Solid-5-The-Phantom-Pain-Trailer.jpg └── Metal_Gear_Solid_V_Screenshot.jpg 5 directories, 6 files raiden@toluca :~/projects/test$ grunt Running "imagemin:dynamic" (imagemin) task ✔ publicImg/upload/folder1/2499085-metal-gear-solid-5-exteded-e3.jpg (saved 8.99 kB - 7%) ✔ publicImg/upload/folder2/metal-gear-solid-5-ground-zeroes-collectibles.jpg (saved 8.5 kB - 25%) ✔ publicImg/upload/folder3/2574364-8751006069-14019.jpg (already optimized) ✔ publicImg/upload/folder3/Metal-Gear-Solid-5-The-Phantom-Pain-Trailer.jpg (saved 2.22 kB - 3%) ✔ publicImg/upload/folder3/Metal_Gear_Solid_V_Screenshot.jpg (already optimized) Minified 5 images (saved 19.71 kB) Done, without errors. raiden@toluca :~/projects/test$ tree publicImages/ publicImages/ └── upload ├── folder1 │  └── 2499085-metal-gear-solid-5-exteded-e3.jpg ├── folder2 │  └── metal-gear-solid-5-ground-zeroes-collectibles.jpg └── folder3 ├── 2574364-8751006069-14019.jpg ├── Metal-Gear-Solid-5-The-Phantom-Pain-Trailer.jpg └── Metal_Gear_Solid_V_Screenshot.jpg 4 directories, 5 files 

You may have problems installing grunt-contrib-imagemin. Try making npm rebuild grunt-contrib-imagemin and see if there are any errors. Or update the module on npm update grunt-contrib-imagemin .

+2
source

All Articles