一口吞下批量调整大小



我需要制作不同分辨率的图像版本。然而,我的gump代码的输出却一团糟——有些文件丢失了,有些图片保存在不同的文件名下。我知道这与异步执行有关,但我对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'))
    });

尝试使用gulp-responsed创建yout任务-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'));
});

来自文件:

吞咽响应图像需要GraphicsMagick才能正常工作。安装很简单:Ubuntu:apt-get-install graphicsmagickMac OS X(使用Homebrew):brew安装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'));
 });

相关内容

  • 没有找到相关文章

最新更新