File tree Expand file tree Collapse file tree 3 files changed +24
-19
lines changed Expand file tree Collapse file tree 3 files changed +24
-19
lines changed Original file line number Diff line number Diff line change @@ -276,7 +276,8 @@ describe('uploadcareLoader', () => {
276
276
'https://test-public-key.ucr.io/-/format/jpeg/-/stretch/off/-/progressive/yes/-/resize/5000x/-/quality/normal/https:/example.com/image.png'
277
277
) ;
278
278
279
- // // Jpg image by extension with auto format. Should be max 5000 width.
279
+ // // Jpg image by extension with auto format.
280
+ // // Should be max 5000 width with /format/jpeg/ forced.
280
281
281
282
src = 'https:/example.com/image.jpg' ;
282
283
@@ -292,7 +293,7 @@ describe('uploadcareLoader', () => {
292
293
} ) ;
293
294
294
295
expect ( result ) . toBe (
295
- 'https://test-public-key.ucr.io/-/format/auto /-/stretch/off/-/progressive/yes/-/resize/5000x/-/quality/normal/https:/example.com/image.jpg'
296
+ 'https://test-public-key.ucr.io/-/format/jpeg /-/stretch/off/-/progressive/yes/-/resize/5000x/-/quality/normal/https:/example.com/image.jpg'
296
297
) ;
297
298
298
299
removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' ) ;
Original file line number Diff line number Diff line change @@ -4,33 +4,30 @@ import {
4
4
} from './constants' ;
5
5
6
6
/**
7
- * Merge user parameters with default parameters , so that user parameters have higher priority.
7
+ * Merge transformation parameters from `a` and `b` , so that `b` parameters have higher priority.
8
8
*
9
- * @param {string[] } defaultParams
10
- * @param {string[] } userParams
9
+ * @param {string[] } a
10
+ * @param {string[] } b
11
11
* @returns {string[] }
12
12
*/
13
- export function mergeParams (
14
- defaultParams : string [ ] ,
15
- userParams : string [ ]
16
- ) : string [ ] {
17
- const resultParams = defaultParams ;
13
+ export function mergeParams ( a : string [ ] , b : string [ ] ) : string [ ] {
14
+ const resultParams = [ ...a ] ;
18
15
19
- for ( let i = 0 ; i < userParams . length ; i ++ ) {
20
- const [ userParam ] = _parseUploadcareTransformationParam ( userParams [ i ] ) ;
16
+ for ( let i = 0 ; i < b . length ; i ++ ) {
17
+ const [ userParam ] = _parseUploadcareTransformationParam ( b [ i ] ) ;
21
18
22
19
let hasBeenReplaced = false ;
23
20
for ( let j = 0 ; j < resultParams . length ; j ++ ) {
24
21
if ( resultParams [ j ] . startsWith ( userParam ) ) {
25
- resultParams [ j ] = userParams [ i ] ;
22
+ resultParams [ j ] = b [ i ] ;
26
23
hasBeenReplaced = true ;
27
24
break ;
28
25
}
29
26
}
30
27
31
28
// If the param is new, just add it.
32
29
if ( ! hasBeenReplaced ) {
33
- resultParams . push ( userParams [ i ] ) ;
30
+ resultParams . push ( b [ i ] ) ;
34
31
}
35
32
}
36
33
Original file line number Diff line number Diff line change @@ -2,7 +2,8 @@ import { ImageLoaderProps } from 'next/image';
2
2
import {
3
3
DEFAULT_CDN_DOMAIN ,
4
4
DEFAULT_PARAMS ,
5
- NOT_PROCESSED_EXTENSIONS
5
+ NOT_PROCESSED_EXTENSIONS ,
6
+ MAX_OUTPUT_IMAGE_DIMENSION
6
7
} from './constants' ;
7
8
import {
8
9
convertToUploadcareQualityString ,
@@ -81,13 +82,19 @@ export function uploadcareLoader({
81
82
requestedFormat === 'jpeg' ||
82
83
( requestedFormat === 'auto' && isJpegExtension ( extension ) ) ;
83
84
const maxResizeWidth = getMaxResizeWidth ( width , isJpeg ) ;
85
+ const forceJpeg = isJpeg && maxResizeWidth > MAX_OUTPUT_IMAGE_DIMENSION ;
84
86
85
- const basicParams = DEFAULT_PARAMS . concat ( [
87
+ const basicParams = DEFAULT_PARAMS . concat (
86
88
`resize/${ maxResizeWidth } x` ,
87
89
`quality/${ qualityString } `
88
- ] ) ;
89
-
90
- const params = mergeParams ( basicParams , userParams ) ;
90
+ ) ;
91
+ const formatOverrideParams = [
92
+ `format/${ forceJpeg ? 'jpeg' : requestedFormat } `
93
+ ] ;
94
+ const params = mergeParams (
95
+ mergeParams ( basicParams , userParams ) ,
96
+ formatOverrideParams
97
+ ) ;
91
98
92
99
const apiParamsString = '/-/' + params . join ( '/-/' ) + '/' ;
93
100
You can’t perform that action at this time.
0 commit comments