Skip to content

Commit 2e93d47

Browse files
authored
fix: force format for large jpegs (#11)
* chore: refactor mergeParams * fix: force jpeg format for jpeg images > 3000px * chore: enable test * chore: rename var
1 parent 0d72b63 commit 2e93d47

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

src/__tests__/uploadcare-loader.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ describe('uploadcareLoader', () => {
276276
'https://test-public-key.ucr.io/-/format/jpeg/-/stretch/off/-/progressive/yes/-/resize/5000x/-/quality/normal/https:/example.com/image.png'
277277
);
278278

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.
280281

281282
src = 'https:/example.com/image.jpg';
282283

@@ -292,7 +293,7 @@ describe('uploadcareLoader', () => {
292293
});
293294

294295
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'
296297
);
297298

298299
removeEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY');

src/utils/helpers.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,30 @@ import {
44
} from './constants';
55

66
/**
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.
88
*
9-
* @param {string[]} defaultParams
10-
* @param {string[]} userParams
9+
* @param {string[]} a
10+
* @param {string[]} b
1111
* @returns {string[]}
1212
*/
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];
1815

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]);
2118

2219
let hasBeenReplaced = false;
2320
for (let j = 0; j < resultParams.length; j++) {
2421
if (resultParams[j].startsWith(userParam)) {
25-
resultParams[j] = userParams[i];
22+
resultParams[j] = b[i];
2623
hasBeenReplaced = true;
2724
break;
2825
}
2926
}
3027

3128
// If the param is new, just add it.
3229
if (!hasBeenReplaced) {
33-
resultParams.push(userParams[i]);
30+
resultParams.push(b[i]);
3431
}
3532
}
3633

src/utils/loader.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { ImageLoaderProps } from 'next/image';
22
import {
33
DEFAULT_CDN_DOMAIN,
44
DEFAULT_PARAMS,
5-
NOT_PROCESSED_EXTENSIONS
5+
NOT_PROCESSED_EXTENSIONS,
6+
MAX_OUTPUT_IMAGE_DIMENSION
67
} from './constants';
78
import {
89
convertToUploadcareQualityString,
@@ -81,13 +82,19 @@ export function uploadcareLoader({
8182
requestedFormat === 'jpeg' ||
8283
(requestedFormat === 'auto' && isJpegExtension(extension));
8384
const maxResizeWidth = getMaxResizeWidth(width, isJpeg);
85+
const forceJpeg = isJpeg && maxResizeWidth > MAX_OUTPUT_IMAGE_DIMENSION;
8486

85-
const basicParams = DEFAULT_PARAMS.concat([
87+
const basicParams = DEFAULT_PARAMS.concat(
8688
`resize/${maxResizeWidth}x`,
8789
`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+
);
9198

9299
const apiParamsString = '/-/' + params.join('/-/') + '/';
93100

0 commit comments

Comments
 (0)