Skip to content

Commit

Permalink
Fix parsing empty and zero width/height value
Browse files Browse the repository at this point in the history
- Fix Sharp error when zero is given
- Remove hexadecimal width / height value support
- Add tests for empty value
  • Loading branch information
martijngastkemper committed Aug 3, 2023
1 parent c516e79 commit ebc61ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 16 additions & 0 deletions test/resizeImage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ describe("resizeImage", () => {
const output = await resizeImage(input, "png", "png", 50);
expect(output.equals(expectedOutput)).toBe(true);
});
it("It can handle an empty string height", async () => {
const input = fs.readFileSync(__dirname + "/fixtures/pixelme.png");
const expectedOutput = fs.readFileSync(
__dirname + "/fixtures/pixelme50x.png"
);
const output = await resizeImage(input, "png", "png", "50", "");
expect(output.equals(expectedOutput)).toBe(true);
});
it("It can handle height 0", async () => {
const input = fs.readFileSync(__dirname + "/fixtures/pixelme.png");
const expectedOutput = fs.readFileSync(
__dirname + "/fixtures/pixelme50x.png"
);
const output = await resizeImage(input, "png", "png", "50", "0");
expect(output.equals(expectedOutput)).toBe(true);
});
});

describe("Given an image with Orientation header", () => {
Expand Down
11 changes: 7 additions & 4 deletions util/resizeImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ function resizeImage(

const resizeOptions = { fit: fit };

if (width) {
resizeOptions.width = parseInt(width);
const parsedWidth = width ? parseInt(width, 10) : 0;
if (parsedWidth > 0) {
resizeOptions.width = parsedWidth;
}
if (height) {
resizeOptions.height = parseInt(height);

const parsedHeight = height ? parseInt(height, 10) : 0;
if (parsedHeight > 0) {
resizeOptions.height = parsedHeight;
}

const sharp = SHARP(body).withMetadata().resize(resizeOptions);
Expand Down

0 comments on commit ebc61ab

Please sign in to comment.