Skip to content

Commit 3938524

Browse files
authored
fix: Do not upsize the images in ImageCompression (#903)
### Issue [CLNP-1832](https://sendbird.atlassian.net/browse/CLNP-1832) * Image Compression feature upsizes the images when the resizing values are greater than the origin length ### Fix * Add optional chaining for the case when a logger doesn't exist * Do not use the resizing value if it's greater than the origin length ### ChangeLog * Do not upsize the images when the resizing values are greater than the origin length
1 parent ceca355 commit 3938524

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/utils/compressImages.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface CompressImageParams {
99
resizingHeight?: number;
1010
}
1111

12-
const compressImage = ({
12+
export const compressImage = ({
1313
imageFile,
1414
compressionRate,
1515
resizingWidth,
@@ -25,13 +25,15 @@ const compressImage = ({
2525

2626
const originWidth = image.width;
2727
const originHeight = image.height;
28-
const widthRatio = originWidth / (resizingWidth || originWidth);
29-
const heightRatio = originHeight / (resizingHeight || originHeight);
28+
let targetResizingWidth = (!resizingWidth || resizingWidth > originWidth) ? originWidth : resizingWidth;
29+
let targetResizingHeight = (!resizingHeight || resizingHeight > originHeight) ? originHeight : resizingHeight;
30+
const widthRatio = originWidth / targetResizingWidth;
31+
const heightRatio = originHeight / targetResizingHeight;
3032

31-
let targetResizingWidth = resizingWidth || originWidth;
32-
let targetResizingHeight = resizingHeight || originHeight;
33-
34-
// Use the more impactful value, so the original images' ratio won't be broken
33+
/**
34+
* Set the target resizing values again with the calculated ratios
35+
* to use the impactful value, so the original images' ratio won't be broken.
36+
*/
3537
if (widthRatio > heightRatio) {
3638
targetResizingHeight = originHeight / (resizingWidth ? widthRatio : 1);
3739
} else if (heightRatio > widthRatio) {
@@ -85,19 +87,19 @@ export const compressImages = async ({
8587
};
8688

8789
if (!(Array.isArray(files) && files.length > 0)) {
88-
logger.warning('utils - compressImages: There are no files.', files);
90+
logger?.warning('utils - compressImages: There are no files.', files);
8991
return result;
9092
}
9193
if (compressionRate < 0 || 1 < compressionRate) {
92-
logger.warning('utils - compressImages: The compressionRate is not acceptable.', compressionRate);
94+
logger?.warning('utils - compressImages: The compressionRate is not acceptable.', compressionRate);
9395
return result;
9496
}
9597

9698
await Promise.all(
9799
files
98100
.map(async (file, index) => {
99101
if (!(file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/jpeg')) {
100-
logger.warning('utils - compressImages: The fileType is not compressible.', { file, index });
102+
logger?.warning('utils - compressImages: The fileType is not compressible.', { file, index });
101103
result.failedIndexes.push(index);
102104
result.compressedFiles.push(file);
103105
return;
@@ -113,11 +115,11 @@ export const compressImages = async ({
113115
result.compressedFiles.push(compressedImage);
114116
} catch (err) {
115117
result.failedIndexes.push(index);
116-
logger.warning('utils - compressImages: Failed to compress image file', { file, err });
118+
logger?.warning('utils - compressImages: Failed to compress image file', { file, err });
117119
}
118120
}),
119121
);
120122

121-
logger.info('utils - compressImages: Finished compressing images', result);
123+
logger?.info('utils - compressImages: Finished compressing images', result);
122124
return result;
123125
};

0 commit comments

Comments
 (0)