我需要重命名一批照片,为它们添加一个索引,比如“Imag-1-TMB”或“Imag-23-TMB”。我已经搜过了,也没找到,甚至都没找到。
这是我的实际代码:
gulp.task('rsz_tmb_menu',function(){
return gulp.src('./zips/**/*.{jpg,JPG}', { base: './zips' })
.pipe(imageResize({
width : width_tmb_menu,
height : height_tmb_menu,
crop : true,
quality : 0.6,
imageMagick : true,
upscale : false
}))
.pipe(gulp.dest('./images/tmb_menu'));
});发布于 2015-10-23 18:35:54
使用吞咽重命名
var rename = require("gulp-rename");然后添加到管道中: gulp.task('rsz_tmb_menu‘)函数(){
var index = 0;
gulp.src('your_glob')
.pipe(your processing func)
.pipe(rename(function (path) {
path.basename += ("-" + index++);
}))
.pipe(...dst...)发布于 2017-04-01 08:15:44
我想这样做是为了附加原始图像…的大小在我的例子中,这是光擦拭的要求。
不幸的是,当我试图添加当前图像的大小时,我也会陷入困境:
var sizeOf = require('image-size');
(...)
.pipe(rename(function (path) {
var dimensions = sizeOf(path);
path.basename += ("-" + dimensions.width + "x" + dimensions.height);
}))引发错误:
node_modules/image-size/lib/index.js:79
throw new TypeError('invalid invocation');回答我自己的问题,以防对别人有帮助
基于http://www.pixeldonor.com/2014/feb/20/writing-tasks-gulpjs/
return gulp.src(...)
.pipe(through.obj(function (chunk, enc, cb) {
dimensions = sizeOf(chunk.path);
extname = Path.extname(chunk.path);
dirname = Path.dirname(chunk.path);
basename = Path.basename(chunk.path, extname);
chunk.path = Path.join(dirname, basename + "-" + dimensions.width + "x" + dimensions.height + extname);
this.push(chunk);
cb(null, chunk);
}))
.pipe(imageResize({
width : 600,
height : 600,
crop : true,
upscale : true
}))
.pipe(gulp.dest(...));https://stackoverflow.com/questions/33221277
复制相似问题