Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch resizing in gulp

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'))
    });
like image 255
Mikhail Vasin Avatar asked Nov 02 '15 22:11

Mikhail Vasin


1 Answers

Try use gulp-responsive to create yout task -https://github.com/dcgauld/gulp-responsive-images .

For example:

var responsive = require('gulp-responsive-images');

gulp.task('resize-all', function() {
    return gulp.src('_img/**/*')
        .pipe(responsive({
            '**/*.*': [{
                width: 2048,
                height: 5000,
                suffix: '-2048'
            }, {
                width: 1536,
                height: 5000,
                suffix: '-1536'
            }]

        }))
        .pipe(gulp.dest('_site/img'));
});

From documentation:

gulp-responsive-images requires GraphicsMagick to function. Installation is simple: Ubuntu: apt-get install graphicsmagick Mac OS X (using Homebrew): brew install graphicsmagick

Or use https://github.com/mahnunchik/gulp-responsive

var responsive = require('gulp-responsive');

gulp.task('resize-all', function() {
     return gulp.src('_img/**/*')
         .pipe(responsive({
             '**/*.*': [{
                 width: 2048,
                 height: 5000,
                 rename: {
                     suffix: '-2048'
                 }
             }, {
                 width: 1536,
                 height: 5000,
                 rename: {
                     suffix: '-1536'
                 }
             }]

         }))
         .pipe(gulp.dest('_site/img'));
 });
like image 89
Bartosz Czerwonka Avatar answered Nov 06 '22 16:11

Bartosz Czerwonka