Skip to content

Commit 39d4cef

Browse files
adamivanczasagidM
authored andcommitted
Fix for files that are rotated after resizing (#1)
* README fix - update the version of sharp * Gitignore added * Auto-rotate images according to EXIF info
1 parent fed8a45 commit 39d4cef

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ or
4747
* * * In **Security** select **Open**, then click **Next**
4848
* * In **Configure function** page
4949
* * * Name a new lambda
50-
* * * In **Runtime** select **Node.js 6.10**
50+
* * * In **Runtime** select **Node.js 8.10**
5151
* * * Upload a _.zip_ file (download it from [releases](https://github.com/sagidM/s3-resizer/releases))
5252
* * * > 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.
5353
* * * Choose role which has permission to put any object or create a new one. To do that

index.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,45 @@ exports.handler = function(event, _context, callback) {
1919

2020

2121
var sizes = options[0].split("x");
22-
var func = options.length > 1 ? options[1] : null;
22+
var action = options.length > 1 ? options[1] : null;
23+
24+
if (action && action !== 'max' && action !== 'min') {
25+
callback(null, {
26+
statusCode: 400,
27+
body: `Unknown func parameter "${action}"\n` +
28+
'For query ".../150x150_func", "_func" must be either empty, "_min" or "_max"',
29+
headers: {"Content-Type": "text/plain"}
30+
});
31+
return;
32+
}
2333

2434
var contentType;
2535
S3.getObject({Bucket: BUCKET, Key: dir + filename})
2636
.promise()
2737
.then(data => {
2838
contentType = data.ContentType;
29-
var img = Sharp(data.Body)
30-
.resize(
31-
sizes[0] === 'AUTO' ? null : parseInt(sizes[0]),
32-
sizes[1] === 'AUTO' ? null : parseInt(sizes[1]));
33-
34-
switch (func){
35-
case 'max': img = img.max(); break;
36-
case 'min': img = img.min(); break;
37-
case null: break;
39+
var width = sizes[0] === 'AUTO' ? null : parseInt(sizes[0]);
40+
var height = sizes[1] === 'AUTO' ? null : parseInt(sizes[1]);
41+
var fit;
42+
switch (action) {
43+
case 'max':
44+
fit = 'inside';
45+
break;
46+
case 'min':
47+
fit = 'outside';
48+
break
3849
default:
39-
callback(null, {
40-
statusCode: 400,
41-
body: `Unknown func parameter "${func}"\n` +
42-
'For query ".../150x150_func", "_func" must be either empty, "_min" or "_max"',
43-
headers: {"Content-Type": "text/plain"}
44-
})
45-
return new Promise(() => {}) // the next then-blocks will never be executed
50+
fit = 'cover';
51+
break;
4652
}
47-
48-
return img.withoutEnlargement().toBuffer();
53+
var options = {
54+
withoutEnlargement: true,
55+
fit
56+
};
57+
return Sharp(data.Body)
58+
.resize(width, height, options)
59+
.rotate()
60+
.toBuffer();
4961
})
5062
.then(result =>
5163
S3.putObject({

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "s3-resizer",
33
"version": "2.0.0",
44
"dependencies": {
5-
"sharp": "^0.20.1"
5+
"sharp": "^0.21.3"
66
},
77
"devDependencies": {
88
"aws-sdk": "^2.36.0"

0 commit comments

Comments
 (0)