@@ -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