gpt4 book ai didi

node.js - gulp批量调整大小

转载 作者:搜寻专家 更新时间:2023-10-31 23:08:48 25 4
gpt4 key购买 nike

我需要制作不同分辨率的图像版本。然而,我的 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 安装 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'));
});

关于node.js - gulp批量调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33488404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com