Skip to content

Commit 13a6d89

Browse files
authored
fix(action): resize images properly (#722)
Co-authored-by: danadajian <danadajian@users.noreply.github.com>
1 parent e1f94fb commit 13a6d89

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

action/dist/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159684,7 +159684,11 @@ async function resizeImageIfNeeded(buffer) {
159684159684
}
159685159685
const image2 = await Jimp.read(buffer);
159686159686
if (width && height) {
159687-
image2.cover({ w: width, h: height });
159687+
const scale = Math.min(width / image2.width, height / image2.height, 1);
159688+
image2.resize({
159689+
w: Math.round(image2.width * scale),
159690+
h: Math.round(image2.height * scale)
159691+
});
159688159692
} else if (width) {
159689159693
image2.resize({ w: width });
159690159694
} else if (height) {

action/dist/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/src/resize.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export async function resizeImageIfNeeded(buffer: Buffer): Promise<Buffer> {
1616

1717
const image = await Jimp.read(buffer);
1818
if (width && height) {
19-
image.cover({ w: width, h: height });
19+
const scale = Math.min(width / image.width, height / image.height, 1);
20+
image.resize({
21+
w: Math.round(image.width * scale),
22+
h: Math.round(image.height * scale)
23+
});
2024
} else if (width) {
2125
image.resize({ w: width });
2226
} else if (height) {

action/test/run.test.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ mock.module('../src/s3-client', () => ({
6969
}));
7070

7171
const jimpImageMock = {
72-
cover: mock(),
72+
width: 400,
73+
height: 300,
7374
resize: mock(),
7475
getBuffer: mock()
7576
};
@@ -178,7 +179,6 @@ describe('main', () => {
178179
readFileMock.mockResolvedValue(Buffer.from('image-data'));
179180
createWriteStreamMock.mockReturnValue(new EventEmitter());
180181
jimpReadMock.mockResolvedValue(jimpImageMock);
181-
jimpImageMock.cover.mockReturnValue(jimpImageMock);
182182
jimpImageMock.resize.mockReturnValue(jimpImageMock);
183183
jimpImageMock.getBuffer.mockResolvedValue(Buffer.from('resized-image'));
184184

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

791791
jimpReadMock.mockResolvedValue(jimpImageMock);
792-
jimpImageMock.cover.mockReturnValue(jimpImageMock);
793792
jimpImageMock.resize.mockReturnValue(jimpImageMock);
794793
jimpImageMock.getBuffer.mockResolvedValue(Buffer.from('resized-image'));
795794
});
@@ -987,7 +986,6 @@ describe('s3-operations', () => {
987986

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

10031001
expect(jimpImageMock.resize).toHaveBeenCalledWith({ h: 150 });
1004-
expect(jimpImageMock.cover).not.toHaveBeenCalled();
10051002
});
10061003

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

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

1015-
expect(jimpImageMock.cover).toHaveBeenCalledWith({ w: 200, h: 150 });
1016-
expect(jimpImageMock.resize).not.toHaveBeenCalled();
1012+
// mock image is 400x300; scale = min(200/400, 150/300, 1) = 0.5
1013+
expect(jimpImageMock.resize).toHaveBeenCalledWith({ w: 200, h: 150 });
10171014
});
10181015
});
10191016

0 commit comments

Comments
 (0)