derhuerst / gulp-scale-images

Gulp plugin to resize each image into multiple smaller variants.
https://github.com/derhuerst/gulp-scale-images#gulp-scale-images
ISC License
13 stars 5 forks source link

How to setup files.scale object in Gulp task? #2

Closed kimfucious closed 6 years ago

kimfucious commented 6 years ago

Hi there,

I was liking the looks of gulp-scale-images over gulp-image-resize, as it doesn't require gm and it seems well maintained.

Sorry for the potentially dumb question, but if all I want to do is run through a directory full of image files to produce a bunch of thumbnails, without using through2 or flat-map, how would I setup the files.scale object?

Here's my gulp task:

gulp.task("thumbs", () => {
  return gulp
    .src(paths.img_thumbs_src)
    .pipe(scaleImages())
    .pipe(gulp.dest(paths.img_thumbs_dist))
    .on("error", gutil.log);
});

When I run it I get:

Error: file.scale must be an object

I'm sure there's an easy answer to this, but I just couldn't glean this from the docs.

Thanks,

Kim

derhuerst commented 6 years ago

Sorry for the potentially dumb question, but if all I want to do is run through a directory full of image files to produce a bunch of thumbnails, without using through2 or flat-map, how would I setup the files.scale object?

You would have to use through2 or flat-map in your case. gulp-scale-images does not decide for you how to scale the images; You may use any logic to generate the file.scale instructions.

The simplest thumbnail generation would probably look like this:

const gulp = require('gulp')
const through2 = require('through2')
const scaleImages = require('gulp-scale-images')

const thumbnail = (file, cb) => {
    file.scale = {maxWidth: 200}
    cb(null, file)
}

gulp.src('path/to/pictures/*.{jpeg,jpg,png}')
.pipe(through2.obj(thumbnail))
.pipe(scaleImages())
.pipe(gulp.dest(…))
derhuerst commented 6 years ago

Please reopen if you have additional questions.