crop it

node v0.12.18
version: 1.0.1
endpointsharetweet
var sc = require('smartcrop'); var fs = require('fs'); var Canvas = require('canvas'), Image = Canvas.Image; var request = require('request'); var url = 'https://scontent-mia1-1.cdninstagram.com/t51.2885-15/e35/891340_341840109273329_991012808_n.jpg?ig_cache_key=MTIxMTc1NjA2Njg2MDY5MzMxNQ%3D%3D.2'; var img = new Image; var cfg = { canvasFactory: function(w, h){ return new Canvas(w, h); }, width: 800, height: 800 }; request.get({url: url, encoding: null}, function(err, response, body) { img.onload = function() { var side = Math.min(img.width, img.height); cfg.width = side; cfg.height = side; sc.crop(img, cfg, function(result) { var canvas = new Canvas(cfg.width, cfg.height); var ctx = canvas.getContext('2d'); var crop = result.topCrop; ctx.patternQuality = 'best'; ctx.filter = 'best'; ctx.drawImage(img, crop.x, crop.y, crop.width, crop.height, 0, 0, canvas.width, canvas.height); console.log(canvas.toBuffer()); }); }; img.onerror = function(err) { console.log(err); }; img.src = body; });
Loading…

no comments

    sign in to comment