-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add support for parameters other then width and height - 500x500 becomes widht:500|height:500 - Add convert parameter to solve the issue with double extensions in the original file. (the reason this change has been made). - Upgrade Jest - Update README.md - Add CHANGELOG.md
- Loading branch information
1 parent
914cb61
commit c4b5ccf
Showing
37 changed files
with
1,285 additions
and
1,388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Upgrade to V4 | ||
|
||
The URL structure has changed to make it possible to add more options. The old URL structure is not supported anymore. But can easily be converted to the new structure. | ||
|
||
Some examples: | ||
|
||
- `/scaled/500x500/foobar.jpg` -> `/scaled/width:500_height:500/foobar.jpg` | ||
- `/scaled/x400/foobar.jpg.webp` -> `/scaled/height:400_convert:webp/foobar.jpg.webp` | ||
|
||
# Upgrade to V3 | ||
|
||
The function uses NodeJS 18 so update your NodeJS version in the deploy workflow. | ||
|
||
Environment variable `AWS_REGION` has been removed. Use the Serverless flag `--region`. For example: `npx serverless deploy --region eu-central-1`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,33 @@ | ||
const fs = require("fs"); | ||
const getInputFormat = require("../util/getInputFormat.js"); | ||
const getOutputFormat = require("../util/getOutputFormat.js"); | ||
const parseOptions = require("../util/parseOptions.js"); | ||
const resizeImage = require("../util/resizeImage.js"); | ||
const pathToParams = require("../util/pathToParams.js"); | ||
const toSharpOptions = require("../util/toSharpOptions.js"); | ||
const { basename } = require("path"); | ||
|
||
const fileInput = process.argv[2]; | ||
if (!fileInput) { | ||
const sourcePath = process.argv[2]; | ||
if (!sourcePath) { | ||
throw new Error("Please provide a path to an image."); | ||
} | ||
const desiredDimensions = process.argv[3]; | ||
if (!desiredDimensions) { | ||
throw new Error("Please provide the dimensions to scale the image."); | ||
} | ||
const fileOutput = process.argv[4]; | ||
if (!fileOutput) { | ||
throw new Error("Please provide a path to write the resized image."); | ||
|
||
const optionsString = process.argv[3]; | ||
if (!optionsString) { | ||
throw new Error( | ||
"Please provide options to scale the image. For example: width:100" | ||
); | ||
} | ||
|
||
if (!fs.existsSync(fileInput)) { | ||
throw new Error(`${fileInput} does not exist.`); | ||
const destinationPath = process.argv[4]; | ||
if (!destinationPath) { | ||
throw new Error("Please provide a path to write the resized image."); | ||
} | ||
|
||
const [size, , outputFormat] = pathToParams( | ||
`${desiredDimensions}/${basename(fileInput)}`, | ||
["jpg", "gif", "png", "jfif", "webp"] | ||
); | ||
const input = fs.readFileSync(sourcePath); | ||
|
||
// Note: both dimensions are optional, but either width or height should always be present. | ||
const dimensions = size.split("x"); | ||
const width = dimensions[0] && parseInt(dimensions[0], 10); | ||
const height = dimensions[1] && parseInt(dimensions[1], 10); | ||
if (!width && !height) { | ||
throw new Error("Unable to deduce dimensions."); | ||
} | ||
const options = parseOptions(optionsString); | ||
|
||
const options = toSharpOptions({ | ||
width, | ||
height, | ||
fit: "cover", | ||
}); | ||
const inputFormat = getInputFormat(destinationFile, options.convert); | ||
const outputFormat = getOutputFormat(destinationFile, options.convert); | ||
|
||
const input = fs.readFileSync(fileInput); | ||
resizeImage(input, outputFormat, options).then((buffer) => | ||
fs.writeFileSync(fileOutput, buffer) | ||
); | ||
resizeImage(input, inputFormat, outputFormat, options.width, options.height) | ||
.then((buffer) => fs.writeFileSync(destinationPath, buffer)) | ||
.then(() => console.log(`Resized image written to ${destinationPath}`)); |
Oops, something went wrong.