Skipper in SailsJS (beta) image resize before upload
Asked Answered
T

2

9

I'm using SailsJS (beta). I'm trying to find a way to use graphicsmagick to take the stream parsed by Skipper in SailsJS-beta to resize the image before calling the Skipper-function req.file('inputName').upload().

My goal is to take my large, original image, and resize it before uploading it. Sails beta have introduced the Skipper-file-parser which are poorly documented (at least I don't understand it). Please help me understand how to resize the image before upload.

This works (code in my controller action):

req.file('fileName').upload('storedImage.png', function(err, files){
  // File is now uploaded to storedImage.png
});

What I want is something like:

// Read the file stream into a file upload
var stream = req.file('fileName');

gm(stream).resize(200, 200).write('storedImage.png', function(err){
  // File is now resized to 200x200 px and uploaded to storedImage.png
});

My problem is: how do I properly fetch the stream from req.file('fileName') to send it to gm?

Thuja answered 5/6, 2014 at 19:59 Comment(0)
C
13

This should work for you:

var Writable = require('stream').Writable;
var resize = require('image-resize-stream')(100); // Or any other resizer

// The output stream to pipe to
var output = require('fs').createWriteStream('storedImage.png');

// Let's create a custom receiver
var receiver = new Writable({objectMode: true});
receiver._write = function(file, enc, cb) {
  file.pipe(resize).pipe(output);

  cb();
};

Now in your action you just have to use your receiver:

req.file('fileName').upload(receiver, function(err, files){
  // File is now resized to 100px width and uploaded to ./storedImage.png
});

I have a feeling that Skipper's API is going to change, a lot, but this will work for now (with v0.1.x).

UPDATE

Specifically, if using gm for resizing, it'll be something like this:

var gm = require('gm');
var Writable = require('stream').Writable;

// The output stream to pipe to
var output = require('fs').createWriteStream('storedImage.png');

// Let's create a custom receiver
var receiver = new Writable({objectMode: true});
receiver._write = function(file, enc, cb) {
  gm(file).resize('200', '200').stream().pipe(output);

  cb();
};
Castera answered 12/6, 2014 at 16:40 Comment(1)
How would I also upload that file to s3 with skipper s3. I'm trying to return the skipper-s3 parameters to the .uploadBasic
R
1

I had problems with @bredikhin solution so I dig deeper into this and found this thread very helpful: Uploading files using Skipper with Sails.js v0.10 - how to retrieve new file name

I just changed one line of his Uploader:

[...]    

file.pipe(outputs);

[...]

into:

gm(file).resize(200, 200).stream().pipe(outputs);

and this does the trick.

I wrote this answer because it may be helpful for someone.

Randirandie answered 21/6, 2015 at 9:51 Comment(1)
Isn't that exactly what @Castera said in his updated post?Thuja

© 2022 - 2024 — McMap. All rights reserved.