UNPKG

easy_rackimg

Version:

A simple way to resize, crop images and to update to rackspace cloudfiles

70 lines (63 loc) 2.16 kB
function uploadPhoto(file, callback){ var easyimg = require('easyimage'); var fs = require("fs"); var img_config = JSON.parse(fs.readFileSync("img_config.json")); var CROPWIDTH = img_config.cropwidth; var CROPHEIGHT = img_config.cropheight; var GRAVITY = img_config.gravity; var WIDTH = img_config.width; var HEIGHT = img_config.height; var X = img_config.x; var Y = img_config.y; var SRC = file; var DST = 'c'+SRC; var DST_RES = 's'+SRC; var USER = img_config.user; var KEY = img_config.key; // --------------------------- var rackit = require('rackit'); rackit.init({ 'user' : "weblife808", 'key' : "6e486eb1cee697c3e69ea464678cb721" }, function(err) { rackit.add(__dirname + "/" + SRC, function(err, cloudpath) { // Get the CDN URI of the file console.log("Normal image was uploaded: " + rackit.getURI(cloudpath)); }); easyimg.crop( { src:SRC, dst:DST, cropwidth:CROPWIDTH, cropheight:CROPHEIGHT, gravity:GRAVITY, x:X, y:Y }, function(err, stdout, stderr) { if (err) throw err; if (!err) { rackit.add(__dirname + "/" + DST, function(err, cloudpath) { // Get the CDN URI of the file console.log("Cropped image was uploaded: " + rackit.getURI(cloudpath)); // The cloudpath parameter uniquely identifies this file, and is used by other Rackit methods to manipulate it. // We should probably store the cloudpath somewhere. }); console.log('Cropped'); } } ); easyimg.resize({src:SRC, dst:DST_RES, width:WIDTH, height:HEIGHT}, function(err, stdout, stderr) { if (err) throw err; if (!err) { rackit.add(__dirname + "/" + DST_RES, function(err, cloudpath) { // Get the CDN URI of the file console.log("Small image was uploaded: " + rackit.getURI(cloudpath)); // The cloudpath parameter uniquely identifies this file, and is used by other Rackit methods to manipulate it. // We should probably store the cloudpath somewhere. }); console.log('Resized to '+ WIDTH+"x"+HEIGHT); } }); } ); } module.exports.upload = uploadPhoto;