Closed btmdave closed 3 years ago
I've been trying to do this too. Multer-Imager (https://github.com/Alexandre-io/multer-imager ) allows you to include Graphics Magick Options -- but so far I've only been able to change the width and height of the image -but I can't get cropping or any of the other Graphics Magick tricks to work with it.
Yeah, for that one they are explicitly defining resize, looks like that's only option. For this, I ended up just using multer, then doing what transforms i needed and using aws sdk directly to upload.
How are you doing the transforms? Using GM (graphics magick) - or using the AWS SDK?
I actually forked multer-imager - and added a call to gm.crop -- after their call to gm.resize -- which actually works nicely - except I've still been having a hard time finding decent documentation on GM itself. (I didn't have much luck with the AWS SDK documentation either though...)
-- Multer-imager has a _handleFile method on the S3Storage prototype that makes the file available as a stream -so that you can pipe it to gm. This is the code I added to check if you've included cropping parameters in the options object:
var outStream = self.s3fs.createWriteStream(filePath, s3options);
if (self.options.crop.width && self.options.gm.crop.height) {
gm(file.stream)
.crop(self.options.gm.crop.width, self.options.gm.crop.height, self.options.gm.crop.x, self.options.gm.crop.y)
.stream(self.options.gm.format || DEFAULT_FORMAT)
.pipe(outStream);
}
if ( self.options.gm.width && self.options.gm.height) {
gm(file.stream)
.crop(self.options.gm.crop.width, self.options.gm.crop.height, self.options.gm.crop.x, self.options.gm.crop.y)
.stream(self.options.gm.format || DEFAULT_FORMAT)
.pipe(outStream); }
The problem with this approach (my code) however is that it doesn't allow you to change the sequence of the gm pipes -- (since gm was written originally as a CLI tool - it's built around the idea of arrange the pipes in various sequences to accomplish complex tasks). Also - it requires you to know what the dimensions are that you're wanting to crop -- which isn't very useful because each image you upload will have different dimensions.
I'm very curious to know more about your solution, btmdave. Thanks!
you gotta use this
npm install multer-s3-transform
Hey, I submitted a PR that supports transformations. You can find the code here in the meantime: https://github.com/ranihorev/multer-s3
I would recommend using Multer 3.x if you need this...
Closing due to inactivity...
Is his project still available? Seems to route here, not sure if there's a new way to transform buffer prior to sending to s3?