Skip to content

Commit

Permalink
Fix for files that are rotated after resizing (#1)
Browse files Browse the repository at this point in the history
* README fix - update the version of sharp

* Gitignore added

* Auto-rotate images according to EXIF info
  • Loading branch information
adamivancza authored and sagidM committed Feb 5, 2019
1 parent fed8a45 commit 39d4cef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ or
* * * In **Security** select **Open**, then click **Next**
* * In **Configure function** page
* * * Name a new lambda
* * * In **Runtime** select **Node.js 6.10**
* * * In **Runtime** select **Node.js 8.10**
* * * Upload a _.zip_ file (download it from [releases](https://github.com/sagidM/s3-resizer/releases))
* * * > You'll also need to set up two **Environment variables**, with _BUCKET_ and _URL_ as keys. But in this time, you don't know about that _URL_. It is **endpoint** which you'll see below.
* * * Choose role which has permission to put any object or create a new one. To do that
Expand Down
50 changes: 31 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,45 @@ exports.handler = function(event, _context, callback) {


var sizes = options[0].split("x");
var func = options.length > 1 ? options[1] : null;
var action = options.length > 1 ? options[1] : null;

if (action && action !== 'max' && action !== 'min') {
callback(null, {
statusCode: 400,
body: `Unknown func parameter "${action}"\n` +
'For query ".../150x150_func", "_func" must be either empty, "_min" or "_max"',
headers: {"Content-Type": "text/plain"}
});
return;
}

var contentType;
S3.getObject({Bucket: BUCKET, Key: dir + filename})
.promise()
.then(data => {
contentType = data.ContentType;
var img = Sharp(data.Body)
.resize(
sizes[0] === 'AUTO' ? null : parseInt(sizes[0]),
sizes[1] === 'AUTO' ? null : parseInt(sizes[1]));

switch (func){
case 'max': img = img.max(); break;
case 'min': img = img.min(); break;
case null: break;
var width = sizes[0] === 'AUTO' ? null : parseInt(sizes[0]);
var height = sizes[1] === 'AUTO' ? null : parseInt(sizes[1]);
var fit;
switch (action) {
case 'max':
fit = 'inside';
break;
case 'min':
fit = 'outside';
break
default:
callback(null, {
statusCode: 400,
body: `Unknown func parameter "${func}"\n` +
'For query ".../150x150_func", "_func" must be either empty, "_min" or "_max"',
headers: {"Content-Type": "text/plain"}
})
return new Promise(() => {}) // the next then-blocks will never be executed
fit = 'cover';
break;
}

return img.withoutEnlargement().toBuffer();
var options = {
withoutEnlargement: true,
fit
};
return Sharp(data.Body)
.resize(width, height, options)
.rotate()
.toBuffer();
})
.then(result =>
S3.putObject({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "s3-resizer",
"version": "2.0.0",
"dependencies": {
"sharp": "^0.20.1"
"sharp": "^0.21.3"
},
"devDependencies": {
"aws-sdk": "^2.36.0"
Expand Down

0 comments on commit 39d4cef

Please sign in to comment.