Skip to content

Commit dfef78e

Browse files
committedSep 17, 2019
implementing qs-to-sharp
1 parent 9f117a3 commit dfef78e

File tree

1 file changed

+51
-26
lines changed

1 file changed

+51
-26
lines changed
 

‎example/src/handler.js

+51-26
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,68 @@
11
const sharp = require('sharp')
22
const path = require('path')
33
const fs = require('fs')
4+
const qsToSharp = require('@sensio/qs-to-sharp')
5+
/**
6+
* Read the LUCKY image file
7+
*/
8+
function readTheLuckyFile() {
9+
// Read the file to a buffer
10+
const imagePath = path.resolve('./src/lucky.jpg')
11+
const imgBuf = fs.readFileSync(imagePath)
12+
return imgBuf
13+
}
414

515
exports.handler = async event => {
616
try {
7-
// Read the file to a buffer
8-
const imagePath = path.resolve('./src/lucky.jpg')
9-
const imgBuf = fs.readFileSync(imagePath)
10-
1117
// Extract the qs from the event
12-
const { queryStringParameters } = event
18+
let queryStringParameters = event.queryStringParameters || {}
1319
console.log(`Got query params `, queryStringParameters)
14-
const { width, height, metadata, convolve } = queryStringParameters
20+
let { width, height, metadata, convolve } = queryStringParameters
21+
22+
const operations = qsToSharp.transform(queryStringParameters)
23+
console.log(operations)
1524

1625
// Let's create the sharp instance
17-
const img = sharp(imgBuf)
26+
const img = sharp(readTheLuckyFile())
1827

19-
if (metadata === 'true') {
20-
img.withMetadata()
21-
}
28+
for (const func in operations) {
29+
if (operations.hasOwnProperty(func)) {
30+
const options = operations[func]
31+
console.log(func, options)
2232

23-
img.resize({
24-
width: parseInt(width, 10) || 800,
25-
height: parseInt(height, 10),
26-
fit: sharp.fit.cover,
27-
position: sharp.strategy.entropy
28-
})
29-
30-
// for fun of it 😎
31-
if (convolve === 'true') {
32-
img.convolve({
33-
width: 3,
34-
height: 3,
35-
kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1]
36-
})
33+
if (options) {
34+
// console.log(`calling image[${func}](${options})`)
35+
img[func](options)
36+
} else {
37+
// console.log(`calling image[${func}]()`)
38+
img[func]()
39+
}
40+
}
3741
}
3842

39-
// transform to the webp format
40-
img.webp()
43+
// if (metadata === '1') {
44+
// img.withMetadata()
45+
// }
46+
47+
// img.resize({
48+
// width: parseInt(width, 10) || 800,
49+
// height: parseInt(height, 10),
50+
// fit: sharp.fit.cover,
51+
// withoutEnlarge: true,
52+
// position: sharp.strategy.entropy
53+
// })
54+
55+
// // for fun of it 😎
56+
// if (convolve) {
57+
// img.convolve({
58+
// width: 3,
59+
// height: 3,
60+
// kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1]
61+
// })
62+
// }
63+
64+
// // transform to the webp format
65+
// img.webp()
4166

4267
// last thing is to get the buffer of cloned instance
4368
const image = await img.clone().toBuffer()

0 commit comments

Comments
 (0)
Please sign in to comment.