Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion action/dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -159684,7 +159684,11 @@ async function resizeImageIfNeeded(buffer) {
}
const image2 = await Jimp.read(buffer);
if (width && height) {
image2.cover({ w: width, h: height });
const scale = Math.min(width / image2.width, height / image2.height, 1);
image2.resize({
w: Math.round(image2.width * scale),
h: Math.round(image2.height * scale)
});
} else if (width) {
image2.resize({ w: width });
} else if (height) {
Expand Down
2 changes: 1 addition & 1 deletion action/dist/main.js.map

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion action/src/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export async function resizeImageIfNeeded(buffer: Buffer): Promise<Buffer> {

const image = await Jimp.read(buffer);
if (width && height) {
image.cover({ w: width, h: height });
const scale = Math.min(width / image.width, height / image.height, 1);
image.resize({
w: Math.round(image.width * scale),
h: Math.round(image.height * scale)
});
} else if (width) {
image.resize({ w: width });
} else if (height) {
Expand Down
13 changes: 5 additions & 8 deletions action/test/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ mock.module('../src/s3-client', () => ({
}));

const jimpImageMock = {
cover: mock(),
width: 400,
height: 300,
resize: mock(),
getBuffer: mock()
};
Expand Down Expand Up @@ -178,7 +179,6 @@ describe('main', () => {
readFileMock.mockResolvedValue(Buffer.from('image-data'));
createWriteStreamMock.mockReturnValue(new EventEmitter());
jimpReadMock.mockResolvedValue(jimpImageMock);
jimpImageMock.cover.mockReturnValue(jimpImageMock);
jimpImageMock.resize.mockReturnValue(jimpImageMock);
jimpImageMock.getBuffer.mockResolvedValue(Buffer.from('resized-image'));

Expand Down Expand Up @@ -789,7 +789,6 @@ describe('s3-operations', () => {
createWriteStreamMock.mockReturnValue(new EventEmitter());

jimpReadMock.mockResolvedValue(jimpImageMock);
jimpImageMock.cover.mockReturnValue(jimpImageMock);
jimpImageMock.resize.mockReturnValue(jimpImageMock);
jimpImageMock.getBuffer.mockResolvedValue(Buffer.from('resized-image'));
});
Expand Down Expand Up @@ -987,7 +986,6 @@ describe('s3-operations', () => {

expect(jimpReadMock).toHaveBeenCalled();
expect(jimpImageMock.resize).toHaveBeenCalledWith({ w: 200 });
expect(jimpImageMock.cover).not.toHaveBeenCalled();
expect(putObjectMock).toHaveBeenCalledWith(
expect.objectContaining({ Body: Buffer.from('resized-image') })
);
Expand All @@ -1001,19 +999,18 @@ describe('s3-operations', () => {
await uploadAllImages('abc123');

expect(jimpImageMock.resize).toHaveBeenCalledWith({ h: 150 });
expect(jimpImageMock.cover).not.toHaveBeenCalled();
});

it('should resize using cover when both resize-width and resize-height are set', async () => {
it('should resize to fit within bounds when both resize-width and resize-height are set', async () => {
s3InputMap['resize-width'] = '200';
s3InputMap['resize-height'] = '150';
globMock.mockResolvedValue(['component/new.png']);

const { uploadAllImages } = await getS3Operations();
await uploadAllImages('abc123');

expect(jimpImageMock.cover).toHaveBeenCalledWith({ w: 200, h: 150 });
expect(jimpImageMock.resize).not.toHaveBeenCalled();
// mock image is 400x300; scale = min(200/400, 150/300, 1) = 0.5
expect(jimpImageMock.resize).toHaveBeenCalledWith({ w: 200, h: 150 });
});
});

Expand Down
Loading