resize-stream

Resize images with streams

resize-stream

Resizing images can suck. This library hopes to make that better.

Depends on ImageMagick, so make sure you have that installed

var rstream = require('resize-stream');
 
// From a file path
rstream.resizeFile('cat.jpg'
  , {width: 50, height: 50} // See options documentation for more
  , function (err, outStream, contentLength) {
    // outStream is a readable stream
    // contentLength makes it easy to .pipe() to S3
  });
 
// From a multipart form request
rstream.resizeReq(req
  , {width: 50, height: 50}
  , function (err, outStream, contentLength) {
    // etc
  });
 
// From a stream
rstream.resizeStream(fs.createReadStream('cat.jpg')
  , {width: 50, height: 50}
  , function (err, outStream, contentLength) {
    // etc
  });

The second parameter is always an optional options hash. See the examples below for a visual guide.

Option NameDefault ValueEffect
width320pxThe target width of the output image in pixels
height320pxThe target height of the output image in pixels
fitfalseFit the input image inside the bounds of the output image
croptrueCrop the image instead of changing the aspect ratio or fitting
autoOrienttrueRotate the image using its EXIF data

Since these are hard to explain in words, here are some visual examples.

Credit

Target DimensionsCropFitResultOutput Dimensions
200x400pxtrueIgnored200x400px
200x400pxfalsefalse200x400px
200x400pxfalsetrue200x200px
  • Stream API kind of stinks, we should be a duplex stream.
  • Finer control over the imagemagick commands

The MIT License (MIT)

Copyright (c) 2013 Ben Ng

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.