app.post('/cropImage', function(req, res, next) { console.log(req.body); var cwidth = parseFloat(req.body.width); var cheight = parseFloat(req.body.height) var cx = parseFloat(req.body.left) var cy = parseFloat(req.body.top) var wr = cwidth/100; var vr = cheight/100; var scaleWidth = parseFloat(req.body.originalWidth)/wr; var scaleHeight = parseFloat(req.body.originalHeight)/vr; var xp = cx/wr; var yp = cy/vr; var makeName = crypto.randomBytes(8).toString('hex')+"_"+(new Date).getTime(); var sendData = { photoUrl: "profileImageOriginal/"+makeName+".png", thumbnailUrl : "profileImageThumbnail/"+makeName+"_thumb.png" } easyimg.crop({src:"public/"+req.body.url, dst:"public/profileImageOriginal/"+makeName+".png", cropwidth: cwidth, cropheight:cheight, gravity: 'NorthWest', x: cx, y: cy },function(err, image) { if (err) throw err; console.log('cropped: ' + image.width + ' x ' + image.height); easyimg.rescrop({ src:"public/"+req.body.url, dst:"public/profileImageThumbnail/"+makeName+"_thumb.png", width: scaleWidth, height: scaleHeight, cropwidth:100, cropheight:100, gravity: 'NorthWest', x: xp, y: yp },function(err, image) { if (err) throw err; console.log('Resized and cropped: ' + image.width + ' x ' + image.height); res.send(sendData); }); }); });
exports.cropImage = function(image,x,y,w,h,width,height,kf,callback) { var width = kf*width, height = kf*height, x = kf*x, y = kf*y, w = kf*w, h = kf*h; easyimg.crop({ src:image, dst:image, cropwidth:w, cropheight:h, x:x, y:y, gravity: 'NorthWest' }).then( function(image) { callback('success'); }, function (err) { callback('error'); } ); }
form.parse(req, function(err, fields, files){ if(err){ return res.json(500, err.message); } var ext = path.extname(files.avatar.name); //var type = files.avatar.type; var tmp = path.basename(files.avatar.path) + ext; var filename = uploadDir + '/' + tmp; var data = { url:tmp }; easyimage.crop({ src:files.avatar.path, dst:filename, cropwidth:100, cropheight:100, x:0, y:0 }, function(err, stdout, stderr) { if (err) { /*return res.json(500,{ error: 'Cannot crop the thumbnail' });*/ return res.json(500,err); } fs.unlink(files.avatar.path, function (err) { if (err) { return res.json(500,{ error: 'Cannot unlink the image' }); } }); res.json(200,data); } ); });
form.parse(req, function(err, fields, files) { var client, file, fileType, fileName, cropPath, uploadToAmazon, uploadToFS, uploadToStore, longURL, shortURL, shortURLRequest, canvas; if (fields.id) { client = req.app.get("clients")[fields.id]; } // Check for either a posted or preuploaded file if (files.file) { file = files.file; } else if (client && client.file && !client.uploading[client.file.path]) { file = client.file; client.uploading[file.path] = true; } if (file) { if (file.size > FILE_SIZE_LIMIT) { res.send("File too large", 500); return; } fileType = file.type; fileName = helpers.generateFileName(fileType.replace("image/", "")); longURL = (req.app.get("localrun") ? "http://" + req.headers.host : req.app.get("domain")) + "/" + fileName; if(knoxClient) { // Start requesting a short URL if(auth.bitly && !req.app.get("localrun")) { shortURLRequest = request("http://api.bitly.com/v3/shorten?login="******"&apiKey=" + auth.bitly.API_KEY + "&longUrl=" + encodeURIComponent(longURL), function(error, response, body){ var json; if (!error && response.statusCode === 200) { json = JSON.parse(body); if (json.status_code === 200) { shortURL = json.data.url; // Store the short URL in the Parse.com database if (auth.parse) { request({ method: "POST", uri: "https://api.parse.com/1/classes/short_url", headers: { "X-Parse-Application-Id": auth.parse.APP_ID, "X-Parse-REST-API-Key": auth.parse.API_KEY }, json: { fileName: fileName, shortURL: shortURL } }); } return; } } shortURL = false; return; }); } uploadToAmazon = function(sourcePath, callback) { knoxClient.putFile( sourcePath, "/" + auth.amazon.S3_IMAGE_FOLDER + fileName, { "Content-Type": fileType }, function(err, putRes) { if (putRes) { fs.unlink(sourcePath); // Remove tmp file if (putRes.statusCode === 200) { if(shortURL === false || !shortURLRequest) { callback({url: longURL}); } else if (shortURL === undefined) { shortURLRequest.on("complete", function(response) { var json; if (response.statusCode === 200) { json = JSON.parse(response.body); if (json.status_code === 200) { callback({url: json.data.url}); return; } } callback({url: longURL}); }).on("error", function() { callback({url: longURL}); }); } else { callback({url: shortURL}); } } else { callback({ error: { message: "Failure", status: putRes.statusCode } }); } } }); }; } uploadToFS = function(sourcePath, callback) { var destFile = options.LOCAL_STORAGE_PATH + fileName; fs.rename(sourcePath, destFile, function(err) { callback({url: longURL}); }); }; uploadToStore = function(sourcePath) { var callback = function(response) { if (!response.error) { helpers.setImageOwner(res, fileName); res.json(response); } else { res.send(response.error, response.status); } }; if (knoxClient) { uploadToAmazon(sourcePath, callback); } else { uploadToFS(sourcePath, callback); } }; if (!fields.cropImage) { uploadToStore(file.path); } else { // Crop the image cropPath = "/tmp/" + fileName; easyimage.crop({ src: file.path, dst: cropPath, cropwidth: fields["crop[width]"], cropheight: fields["crop[height]"], x: fields["crop[x]"], y: fields["crop[y]"], gravity: "NorthWest" }, function() { fs.unlink(file.path); uploadToStore(cropPath); }); } } else { res.send("Missing file", 500); } });
easyimg.info(srcImage).then(function(info){ console.log("Image w: " + info.width); console.log("Image H: " + info.height); console.log("X: " + cropData.x); console.log("Y: " + cropData.y); console.log("H: " + cropData.height); console.log("W: " + cropData.width); //OK easy image is cropping with x,y 0,0 as the center of the image //The parameters I am passing is are from the top corner //So easyimage is adding 1/2 of the width and height to my coord //So I need to subtract //This formula was worked out with pen and paper and trig! //****** DO NOT CHANGE THESE THE NEXT 2 LINES ***************** var x = cropData.x -(info.width - cropData.width)/2; var y = cropData.y -(info.height - cropData.height)/2; x = Math.round(x); y = Math.round(y); //************************************************************* console.log("x: " + x); console.log("y: " + y); easyimg.crop({ src: srcImage, dst: tgtImage, cropwidth: cropData.width, cropheight: cropData.height, x:x, y:y }).then( function(image) { console.log('Cropped: ' + image.width + ' x ' + image.height); //AWS var fileBuffer = fs.readFileSync(tgtImage); var ContentType = getContentTypeByFile(tgtImage); //Put avatars into the avatar folder fileName = "avatars/" + fileName; console.log("S3_BUCKET : " + S3_BUCKET); console.log("Key : " + fileName); console.log("ContentType : " + ContentType); var params = { ACL: 'public-read', Bucket: S3_BUCKET, Key: fileName, Body: fileBuffer, ContentType: ContentType }; var s3 = new aws.S3(); s3.putObject(params, function(error, response) { if (error) return next(error); console.log('file uploaded'); user.avatar = "https://s3-eu-west-1.amazonaws.com/" + S3_BUCKET + "/" + fileName; user.save(function(err,user){ if (err) return next(err); //res.redirect('/profile'); if (req.xhr) { console.log("XHR") res.status(200).send({ state: 200, message: 'User Saved', result: user.avatar}); } else { next(err); } }); }); }, function (err) { console.log(err); }); });