create-thumbnail

This block inputs an image, resizes it to the user-provided dimensions and outputs the result.

Setup:

npm install codeblox jimp

codeblox.json

{
  "name": "create-thumbnail",
  "version": "1.0.0",
  "description": "Creates a thumbnail",
  "keywords": [
    "image",
    "thumbnail"
  ],
  "input": "image",
  "output": "image"
}

codeblox.js

const codeblox = require('codeblox')
const Jimp = require('jimp')

module.exports.default = codeblox((input, options, callback) => {
  try {
    const imgBuffer = new Buffer(input, "binary");
    Jimp.read(imgBuffer, (err, image) => {
      if (err) {
        return callback(err)
      }
      const resized = image
        .scaleToFit(options.Width || 50, options.Height || 50)
        .getBuffer(Jimp.AUTO, (err, data) => {
          callback(null, data.toString('binary'))
        })
    })
  } catch(e) {
    callback(JSON.stringify(e))
  }
})

results matching ""

    No results matching ""