eduardoboucas / smartcrop-lwip

Node module for using smartcrop via lwip

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

smartcrop-lwip

This is an adapter module for using smartcrop.js with node.js using lwip for image decoding.

Installation

npm install --save smartcrop-lwip

API

crop(image, options)

Image: string (path to file) or buffer

Options: options object to be passed to smartcrop

returns: A promise for a cropResult

Example

var request = require('request');
var smartcrop = require('smartcrop-lwip');
var lwip = require('lwip');

function applySmartCrop(src, dest, width, height) {
  request(src, {encoding: null}, function process(error, response, body) {
    if (error) return console.error(error);
    smartcrop.crop(body, {width: width, height: height}).then(function(result) {
      var crop = result.topCrop;

      lwip.open(body, 'jpg', function (err, image) {
        if (err) return console.error(err);

        image.batch()
             .crop(crop.x, crop.y, crop.width + crop.x, crop.y + crop.height)
             .resize(width, height)
             .writeFile(dest, function (err) {
                if (err) return console.error(err);
             });
      });
    });
  });
}

var src = 'https://raw.githubusercontent.com/jwagner/smartcrop-gm/master/test/flower.jpg';
applySmartCrop(src, 'flower-square.jpg', 128, 128);

About

Node module for using smartcrop via lwip


Languages

Language:JavaScript 100.0%