Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/hotfix/jochem-fix-resize-quality…
Browse files Browse the repository at this point in the history
…' into v2.x
  • Loading branch information
martijngastkemper committed May 16, 2023
2 parents 0bfa200 + 0d4f40d commit 8f39997
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ SERVICE_NAME=

BUCKET=
# IMAGE_ACL="public-read"
# IMAGE_QUALITY=80

# Deployment options:
DEPLOYMENT_BUCKET=
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ The following environment variables are mandatory:
- `SERVERLESS_ROLE`: the role assumed by the Lambda function.
- `SERVICE_NAME`: the name of the Lambda function, defaults to "imageScaler".

The following environment variables are optional:

- `QUALITY`: the format option quality setting for all image types. (integer: 1-100)

##### Defining SERVERLESS_ROLE

Create a role which Lambda functions are allowed to assume. Add the following trust relationship:
Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getImageMimetype = require("./util/get-image-mime-type.js");

const S3 = new AWS.S3();

const { BUCKET, IMAGE_ACL } = process.env;
const { BUCKET, IMAGE_ACL, IMAGE_QUALITY } = process.env;
const ALLOWED_EXTENSIONS = ["jpeg", "jpg", "png", "webp", "gif", "svg", "jfif"];

module.exports.handler = async function handler(event, context, callback) {
Expand All @@ -19,10 +19,14 @@ module.exports.handler = async function handler(event, context, callback) {
key,
ALLOWED_EXTENSIONS
);
const quality = parseInt(IMAGE_QUALITY);

console.log(`Using path: ${path}`);
console.log(`Using dimensions: ${size}`);
console.log(`Using output-format: ${outputFormat}`);
if (quality) {
console.log(`Using quality: ${quality}`);
}

// Note: both dimensions are optional, but either width or height should always be present.
const dimensions = size.split("x");
Expand All @@ -46,14 +50,15 @@ module.exports.handler = async function handler(event, context, callback) {
height,
fit: "cover",
});

/**
* Note: resizing SVG doesn't make sense.
* In that case, simply re-upload the original image to the new destination.
*/
const buffer =
outputFormat === "svg" && originalExtension === "svg"
? objectBody
: await resizeImage(objectBody, outputFormat, options);
: await resizeImage(objectBody, outputFormat, options, quality);

/**
* Store the new image on the originally requested path in the bucket.
Expand Down
10 changes: 9 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const dotenv = require("dotenv");
const express = require("express");
const fs = require("fs");
const pathToParams = require("../util/pathToParams.js");
const toSharpOptions = require("../util/toSharpOptions.js");
const resizeImage = require("../util/resizeImage.js");
const getImageMimetype = require("../util/get-image-mime-type.js");

dotenv.config();
const { IMAGE_QUALITY } = process.env;

const app = express();

// TODO nice to make the port configurable.
Expand Down Expand Up @@ -36,10 +40,14 @@ app.get("*", async function (req, res, next) {
res.sendStatus(404);
return;
}
const quality = parseInt(IMAGE_QUALITY);

console.log(`Using path: ${path}`);
console.log(`Using dimensions: ${size}`);
console.log(`Using output-format: ${outputFormat}`);
if (quality) {
console.log(`Using quality: ${quality}`);
}

// Note: both dimensions are optional, but either width or height should always be present.
const dimensions = size.split("x");
Expand Down Expand Up @@ -73,7 +81,7 @@ app.get("*", async function (req, res, next) {
const buffer =
outputFormat === "svg" && originalExtension === "svg"
? objectBody
: await resizeImage(objectBody, outputFormat, options);
: await resizeImage(objectBody, outputFormat, options, quality);

/**
* Redirect the user back to the originally requested path.
Expand Down
1 change: 1 addition & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ provider:
environment:
BUCKET: ${env:BUCKET}
IMAGE_ACL: ${env:IMAGE_ACL,""}
IMAGE_QUALITY: ${env:IMAGE_QUALITY}

functions:
main:
Expand Down
8 changes: 6 additions & 2 deletions util/resizeImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ const SHARP = require("sharp");
* Resize an image.
* Pass it the contents of the original file, the output format and further Sharp options.
*/
function resizeImage(body, outputFormat, sharpOptions) {
function resizeImage(body, outputFormat, sharpOptions, quality) {
const options = { progressive: true };
if (quality) {
options.quality = quality;
}
return SHARP(body)
.withMetadata()
.resize(sharpOptions)
.toFormat(toSharpOutputFormat(outputFormat), { progressive: true })
.toFormat(toSharpOutputFormat(outputFormat), options)
.toBuffer();
}

Expand Down

0 comments on commit 8f39997

Please sign in to comment.