I am trying to efficiently generate responsive images for use on a website, but have some trouble with handling existing files and paths in gulp. The images reside in hierarchically structured folders, where pages
is the root with several subfolders recursing downwards - most of which have images.
My current task looks like this:
gulp.task('responsive', function() {
gulp.src('pages/**/*.{jpg,png}')
.pipe($.plumber({
errorHandler: function (err) {
console.log(err);
this.emit('end');
}
}))
.pipe(responsive({
'**/*.*': [{
width: 2240,
suffix: '-2240',
quality: 70
}, {
width: 1920,
suffix: '-1920',
quality: 70
}, {
width: 1600,
suffix: '-1600',
quality: 70
}, {
width: 1280,
suffix: '-1280',
quality: 70
}, {
width: 1280,
height: 300,
crop: true,
gravity: 'Center',
suffix: '-thumbwide',
quality: 70
}, {
width: 960,
suffix: '-960',
quality: 70
}, {
width: 640,
suffix: '-640',
quality: 70
}, {
width: 480,
suffix: '-480',
quality: 70
}, {
width: 320,
suffix: '-320',
quality: 70
}]
}))
.pipe(gulp.dest(function(file) {
return file.base;
}));
});
There are two main issues with this:
- Images are piped into the same folder as their source, when they would preferably be in an
images
-folder next to the source. - If the task is ran again, it generates new files based on both the sources and the previously generated responsive images.
The task uses gulp-responsive-images, which leverages GraphicsMagick for the resizing. I tried using gulp-changed (set as .pipe($.cached('pages'))
) to resolve issue 2, but it seemed to have no effect.
How can I resolve issue 1 and 2, based on my current setup?