Skip to content

Commit

Permalink
Allow optional cropping of images while uploading;
Browse files Browse the repository at this point in the history
  • Loading branch information
rogierslag committed May 19, 2016
1 parent 882135a commit 95e5952
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const Helpers = {
res.setHeader('Access-Control-Allow-Method', 'GET');
next();
},
send400(res, message = '') {
log.log('warning', `Invalid request was done: ${message}`);
res.statusCode = 400;
res.end(message);
},
send404(res, url, error = true) {
if (error) {
log.log('warning', `404 Error for ${url}`);
Expand Down
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ const Image = {
// Upload the RAW image to disk, stripped of its extension
// First check the token
const sentToken = req.headers['x-token'];
const matches = req.url.match(/^\/(.*)\.([^.]+)$/);
const matches = req.path.match(/^\/(.*)\.([^.]+)$/);
log.log('info', `Requested image upload for image_id ${matches[1]} with token ${sentToken}`);
if (!parsing.supportedFileType(matches[2])) {
helpers.send415(res, matches[2]);
Expand All @@ -295,9 +295,19 @@ const Image = {
const temp_path = files.image.path;
const destination_path = `${config.get('originals_dir')}/${matches[1]}`;

im(temp_path)
.autoOrient()
.write(destination_path, (err) => {
let oriented = im(temp_path).autoOrient();
if ( req.query.x && req.query.y && req.query.width && req.query.height ) {
const x = parseInt(req.query.x, 10);
const y = parseInt(req.query.y, 10);
const width = parseInt(req.query.width, 10);
const height = parseInt(req.query.height, 10);
if ( isNaN(x) || isNaN(y) || isNaN(width) || isNaN(height)) {
helpers.send400('Given crop parameters are invalid');
return;
}
oriented = oriented.crop(width, height, x, y);
}
oriented.write(destination_path, (err) => {
if (err) {
helpers.send500(res, err);
return;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "live-image-resize",
"version": "0.1.1",
"version": "1.1.0",
"description": "Dynamic image resizing service, using AWS S3 as a cache",
"main": "index.js",
"dependencies": {
Expand Down

0 comments on commit 95e5952

Please sign in to comment.