|
1 | 1 | const sharp = require('sharp')
|
2 | 2 | const path = require('path')
|
3 | 3 | 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 | +} |
4 | 14 |
|
5 | 15 | exports.handler = async event => {
|
6 | 16 | try {
|
7 |
| - // Read the file to a buffer |
8 |
| - const imagePath = path.resolve('./src/lucky.jpg') |
9 |
| - const imgBuf = fs.readFileSync(imagePath) |
10 |
| - |
11 | 17 | // Extract the qs from the event
|
12 |
| - const { queryStringParameters } = event |
| 18 | + let queryStringParameters = event.queryStringParameters || {} |
13 | 19 | 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) |
15 | 24 |
|
16 | 25 | // Let's create the sharp instance
|
17 |
| - const img = sharp(imgBuf) |
| 26 | + const img = sharp(readTheLuckyFile()) |
18 | 27 |
|
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) |
22 | 32 |
|
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 | + } |
37 | 41 | }
|
38 | 42 |
|
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() |
41 | 66 |
|
42 | 67 | // last thing is to get the buffer of cloned instance
|
43 | 68 | const image = await img.clone().toBuffer()
|
|
0 commit comments