5

我需要制作不同分辨率的图像版本。但是,我的 gulp 代码的输出是一团糟——一些文件丢失了,一些图片保存在不同的文件名下。我知道它与异步执行有关,但我对 node.js 并不是很熟悉,所以我无法自己弄清楚如何修复它。任何帮助表示赞赏,尤其是解释。

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'))
    });
4

1 回答 1

6

尝试使用 gulp-responsive 创建你的任务 - https://github.com/dcgauld/gulp-responsive-images

例如:

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'));
});

从文档:

gulp-responsive-images 需要 GraphicsMagick 才能运行。安装很简单: Ubuntu:apt-get install graphicsmagick Mac OS X(使用 Homebrew):brew install graphicsmagick

或使用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'));
 });
于 2015-11-02T23:03:57.970 回答