I need to make versions of images in different resolutions. However, the output of my gulp code is a mess - some files are missing, some pictures are saved under different filenames. I know it has something to do with async execution, but I'm not really familiar with node.js, so I can't figure out by myself how to fix it. Any help is appreciated, especially with explanation.
var gulp = require('gulp');
var rename = require('gulp-rename');
var gm = require('gulp-gm');
var originalBasename = '';
gulp.task('resize-all', function () {
return gulp.src(['_img/**/*.{jpg,png}', '_img/*.{jpg,png}'])
.pipe(rename(function (path) {
originalBasename = path.basename;
path.basename += "-2048";
}))
.pipe(gm(function (gmfile) {
return gmfile.resize(2048, 5000);
}))
.pipe(gulp.dest('_site/img'))
.pipe(rename(function (path) {
path.basename = originalBasename; // restore basename
path.basename += "-1536";
}))
.pipe(gm(function (gmfile) {
return gmfile.resize(1536, 5000);
}))
.pipe(gulp.dest('_site/img'))
.pipe(rename(function (path) {
path.basename = originalBasename; // restore basename
path.basename += "-1080";
}))
.pipe(gm(function (gmfile) {
return gmfile.resize(1080, 5000);
}))
.pipe(gulp.dest('_site/img'))
.pipe(rename(function (path) {
path.basename = originalBasename; // restore basename
path.basename += "-750";
}))
.pipe(gm(function (gmfile) {
return gmfile.resize(750, 5000);
}))
.pipe(gulp.dest('_site/img'))
.pipe(rename(function (path) {
path.basename = originalBasename; // restore basename
path.basename += "-320";
}))
.pipe(gm(function (gmfile) {
return gmfile.resize(320, 5000);
}))
.pipe(gulp.dest('_site/img'))
});