Skip to content

Commit 363176b

Browse files
Merge pull request #238 from dharamghevariya/feature-auto-pad-crop-mode
feat: Add support for `auto_pad` cropping mode
2 parents 1987e0a + d3b529a commit 363176b

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

packages/url-loader/src/constants/parameters.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { StringifiablePrimative } from "../lib/utils.js";
66
*/
77
export type CropMode =
88
| "auto"
9+
| "auto_pad"
910
| "crop"
1011
| "fill"
1112
| "fill_pad"

packages/url-loader/src/plugins/cropping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { plugin } from "../lib/plugin.js";
1313
import { isArray } from "../lib/utils.js";
1414
import type { PluginResults } from "../types/plugins.js";
1515

16-
const cropsAspectRatio = ["auto", "crop", "fill", "lfill", "fill_pad", "thumb"];
17-
const cropsGravityAuto = ["auto", "crop", "fill", "lfill", "fill_pad", "thumb"];
16+
const cropsAspectRatio = ["auto", "auto_pad", "crop", "fill", "lfill", "fill_pad", "thumb"];
17+
const cropsGravityAuto = ["auto", "auto_pad", "crop", "fill", "lfill", "fill_pad", "thumb"];
1818
const cropsWithZoom = ["crop", "thumb"];
1919

2020
const DEFAULT_CROP = "limit";

packages/url-loader/tests/plugins/cropping.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,60 @@ describe("Cropping plugin", () => {
411411
`image/upload/c_${options.crop.type},w_${options.width},h_${options.height},x_${options.crop.x},y_${options.crop.y}`,
412412
);
413413
});
414+
415+
it("should apply auto_pad crop mode with explicit gravity auto", () => {
416+
const cldImage = cld.image(TEST_PUBLIC_ID);
417+
const options = {
418+
src: TEST_PUBLIC_ID,
419+
width: 960,
420+
aspectRatio: "16:9",
421+
crop: "auto_pad",
422+
gravity: "auto",
423+
} as const;
424+
const result = CroppingPlugin.apply(cldImage, options);
425+
426+
if (result.options?.resize === undefined)
427+
throw new Error(`Expected result.options.resize to not be undefined`);
428+
429+
expect(result.options?.resize).toBe(
430+
`c_auto_pad,ar_16:9,w_960,g_auto`,
431+
);
432+
});
433+
434+
it("should apply auto_pad crop mode with auto gravity by default", () => {
435+
const cldImage = cld.image(TEST_PUBLIC_ID);
436+
const options = {
437+
src: TEST_PUBLIC_ID,
438+
width: 960,
439+
aspectRatio: "16:9",
440+
crop: "auto_pad",
441+
} as const;
442+
const result = CroppingPlugin.apply(cldImage, options);
443+
444+
if (result.options?.resize === undefined)
445+
throw new Error(`Expected result.options.resize to not be undefined`);
446+
447+
expect(result.options?.resize).toBe(
448+
`c_auto_pad,ar_16:9,w_960,g_auto`,
449+
);
450+
});
451+
452+
it("should apply auto_pad crop mode with width and height", () => {
453+
const cldImage = cld.image(TEST_PUBLIC_ID);
454+
const options = {
455+
src: TEST_PUBLIC_ID,
456+
width: 960,
457+
height: 540,
458+
crop: "auto_pad",
459+
gravity: "auto",
460+
} as const;
461+
const result = CroppingPlugin.apply(cldImage, options);
462+
463+
if (result.options?.resize === undefined)
464+
throw new Error(`Expected result.options.resize to not be undefined`);
465+
466+
expect(result.options?.resize).toBe(
467+
`c_auto_pad,w_960,h_540,g_auto`,
468+
);
469+
});
414470
});

0 commit comments

Comments
 (0)