2

I need to rename a batch of photos adding an index to them, like 'image-1-tmb' or 'image-23-tmb'. I have already searched this and didn't find it, didn't even come close to finding it.

This is my actual code:

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

2 Answers2

1

Use gulp-rename:

var rename = require("gulp-rename");

then add to your pipe: gulp.task('rsz_tmb_menu',function(){

  var index = 0;

gulp.src('your_glob')
  .pipe(your processing func)
  .pipe(rename(function (path) {
  path.basename += ("-" + index++);
 }))
.pipe(...dst...)
Meir
  • 14,081
  • 4
  • 39
  • 47
0

I'd like to do it to append the size of the original image… In my case, this is a requirement of photoswipe.

Trying to do it with gulp, unfortunately, I get stuck even when trying to append the size of the current image:

var sizeOf = require('image-size');

(...)

.pipe(rename(function (path) {
  var dimensions = sizeOf(path);
  path.basename += ("-" + dimensions.width + "x" +  dimensions.height);
 }))

raises an error:

node_modules/image-size/lib/index.js:79
throw new TypeError('invalid invocation');

answering my own question in case it helps someone
based on 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(...));
julou
  • 602
  • 4
  • 12