Skip to content

Commit 93fe3d6

Browse files
Merge pull request #236 from cloudinary-community/b_gen_fill-seed
feat: add seed parameter for b_gen_fill
2 parents dc1ece8 + c5f1e72 commit 93fe3d6

File tree

3 files changed

+77
-18
lines changed

3 files changed

+77
-18
lines changed

packages/url-loader/src/plugins/fill-background.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export declare namespace FillBackgroundPlugin {
2121
crop?: CropMode;
2222
gravity?: Gravity;
2323
prompt?: ListablePrompts;
24+
seed?: number;
2425
}
2526
}
2627

@@ -64,16 +65,26 @@ export const FillBackgroundPlugin = /* #__PURE__ */ plugin({
6465

6566
cldAsset.addTransformation(properties.join(","));
6667
} else if (typeof fillBackground === "object") {
67-
const { crop = defaultCrop, gravity, prompt } = fillBackground;
68+
const { crop = defaultCrop, gravity, prompt, seed } = fillBackground;
6869

69-
const properties = [`ar_${aspectRatio}`, `c_${crop}`];
70+
const bGenFillProperties = [];
7071

7172
if (typeof prompt === "string") {
72-
properties.unshift(`b_gen_fill:${prompt}`);
73-
} else {
74-
properties.unshift(`b_gen_fill`);
73+
bGenFillProperties.push(`prompt_${prompt}`);
7574
}
7675

76+
if (typeof seed === "number") {
77+
bGenFillProperties.push(`seed_${seed}`);
78+
}
79+
80+
const bGenFill = (
81+
bGenFillProperties.length > 0
82+
? `b_gen_fill:${bGenFillProperties.join(';')}`
83+
: "b_gen_fill"
84+
);
85+
86+
const properties = [ bGenFill, `ar_${aspectRatio}`, `c_${crop}`];
87+
7788
if (typeof gravity === "string") {
7889
properties.push(`g_${gravity}`);
7990
}

packages/url-loader/tests/plugins/fill-background.spec.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,54 @@ describe("Plugins", () => {
4141
gravity: "east",
4242
prompt: "pink and purple flowers",
4343
crop: "mpad",
44+
seed: 3
4445
},
4546
} as const;
4647

4748
FillBackgroundPlugin.apply(cldImage, options);
4849

4950
expect(cldImage.toURL()).toContain(
50-
`b_gen_fill:${encodeURIComponent(options.fillBackground.prompt)},ar_${options.width}:${options.height},c_${options.fillBackground.crop},g_${options.fillBackground.gravity}/${TEST_PUBLIC_ID}`,
51+
`b_gen_fill:prompt_${encodeURIComponent(options.fillBackground.prompt)};seed_3,ar_${options.width}:${options.height},c_${options.fillBackground.crop},g_${options.fillBackground.gravity}/${TEST_PUBLIC_ID}`,
52+
);
53+
});
54+
55+
it("should generate with just a seed but no prompt", () => {
56+
const cldImage = cld.image(TEST_PUBLIC_ID);
57+
58+
const options = {
59+
src: TEST_PUBLIC_ID,
60+
width: 800,
61+
height: 600,
62+
fillBackground: {
63+
crop: 'pad',
64+
seed: 3
65+
},
66+
} as const;
67+
68+
FillBackgroundPlugin.apply(cldImage, options);
69+
70+
expect(cldImage.toURL()).toContain(
71+
`b_gen_fill:seed_3,ar_${options.width}:${options.height},c_${options.fillBackground.crop}/${TEST_PUBLIC_ID}`,
72+
);
73+
});
74+
75+
it("should generate with just a prompt but no seed", () => {
76+
const cldImage = cld.image(TEST_PUBLIC_ID);
77+
78+
const options = {
79+
src: TEST_PUBLIC_ID,
80+
width: 800,
81+
height: 600,
82+
fillBackground: {
83+
crop: 'pad',
84+
prompt: 'a herd of llamas'
85+
},
86+
} as const;
87+
88+
FillBackgroundPlugin.apply(cldImage, options);
89+
90+
expect(cldImage.toURL()).toContain(
91+
`b_gen_fill:prompt_${encodeURIComponent(options.fillBackground.prompt)},ar_${options.width}:${options.height},c_${options.fillBackground.crop}/${TEST_PUBLIC_ID}`,
5192
);
5293
});
5394

pnpm-lock.yaml

Lines changed: 19 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)