1.0.2 • Published 5 years ago

@juniorcitizen/convert-to-png v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Image to PNG Conversion Utility (@juniorcitizen/convert-to-png)

simple sharp.js wrapper to apply resize, adjust quality and png conversion to image files

Installation

npm install --save @juniorcitizen/convert-to-png

Usage

const Converter = require('@juniorcitizen/convert-to-png')

// optional quality options
// https://sharp.pixelplumbing.com/en/stable/api-output/#png
const options = {
  compressionLevel: 5,
  quality: 50,
}

/*
 * 1. proportionally resized to be contained within 640x480 pixels
 * 2. quality manipulations (optional)
 * 3. output to specified path
 * 
 * note: images are not enlarged if they are already
 *       smaller than specified
 */
new Converter()
  .init('./fileName1.jpg', {xLimit: 640, yLimit: 480})
  .then(pngImage => pngImage.output('./fileName1.png', options))
  .catch(error => console.log(error))

/*
 * 1. resized to max width 320px and height resized proportionally
 * 2. no quality manipulations
 * 3. output to specified path
 */
new Converter()
  .init('./fileName2.jpg', {xLimit: 320})
  .then(pngImage => pngImage.output('./fileName2.png'))
  .catch(error => console.log(error))

/*
 * 1. no resize
 * 2. quality manipulations (optional)
 * 3. output to buffer
 */
new Converter()
  .init('./fileName1.jpg')
  .then(pngImage => pngImage.output(undefined, options))
  .then(buffer => {
    console.log(buffer)
    return Promise.resolve()
  })
  .catch(error => console.log(error))

API

instance.init(filePath, resizeLimits)

filePath: String - source file path

resizeLimits: {[xLimit: Number], [yLimit: Number]}

instance.resize(xLimit, xLimit)

xLimit: Number

yLimit: Number

instance.output(filePath, options)

filePath: String - destination file path

options: Object - see sharp.js documentations

Notes

  1. Uses lovell/sharp for the image operations, please see their doc for available options.

  2. EXIF information is stripped

  3. image transparency is preserved

  4. if only one limit is defined, the other dimension is proportionally resized

  5. images are not enlarged if already are smaller than the specified dims on both x and y directions