resize-logic

Determines the correct dimensions for resizing and cropping images, without being attached to any particular tool

resize-logic

Determines the correct dimensions for resizing and cropping images, without being attached to any particular tool

require('resize-logic') exports a function that takes a selection of options:

var resize  = require('resize-logic')
var options = {
    height: 200
  , original: [400, 200]
  , crop: true
}
 
var results = resize(options)
  • options.original: the original dimensions of the image as an array, i.e.: [width, height]
  • options.width: the target width of the resized image. Optional, but one of height or width must be included.
  • options.height: the target height of the resized image. Optional, but one of height or width must be included.
  • options.crop: whether or not to crop the image or just scale it. Defaults to false.
  • options.smaller: whether to scale to handle the smallest of width/height or heighest, when both are supplied. Defaults to true, or false if options.crop is enabled.

When called, the function returns a results object with the following properties:

{
    "drawPosition": [-20, 0]
  , "drawDimensions": [400, 200]
  , "canvasDimensions": [360, 200]
}

The x/y position to draw the image (where the origin is the top-left). Supplied as an array, i.e. [x, y].

The width and height to draw the image. Supplied as an array, i.e. [width, height].

The width and height of the actual canvas to draw on. Supplied as an array, i.e. [width, height]

MIT. See LICENSE.md for details.