在 gulp 内批量调整大小
batch resizing in gulp
我需要制作不同分辨率的图像版本。然而,我的 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'))
});
尝试使用 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 安装 graphicsmagick
Mac 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'));
});
我需要制作不同分辨率的图像版本。然而,我的 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'))
});
尝试使用 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 安装 graphicsmagick Mac 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'));
});