Skip to content

Commit 63b01cd

Browse files
committed
refactor: update transformation interface and supported transforms for improved clarity and consistency
1 parent 8fa1219 commit 63b01cd

File tree

4 files changed

+88
-35
lines changed

4 files changed

+88
-35
lines changed
Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/**
22
* @link https://imagekit.io/docs/transformations
33
*/
4-
const supportedTransforms: { [key: string]: string } = {
4+
export const supportedTransforms: { [key: string]: string } = {
5+
// Basic sizing & layout
6+
width: "w",
7+
height: "h",
58
aspectRatio: "ar",
69
background: "bg",
710
border: "b",
@@ -14,14 +17,11 @@ const supportedTransforms: { [key: string]: string } = {
1417
xCenter: "xc",
1518
y: "y",
1619
yCenter: "yc",
17-
height: "h",
18-
width: "w",
1920
format: "f",
2021
videoCodec: "vc",
2122
audioCodec: "ac",
2223
radius: "r",
2324
rotation: "rt",
24-
rotate: "rt",
2525
blur: "bl",
2626
named: "n",
2727
defaultImage: "di",
@@ -31,25 +31,31 @@ const supportedTransforms: { [key: string]: string } = {
3131
endOffset: "eo",
3232
duration: "du",
3333
streamingResolutions: "sr",
34-
eGrayscale: "e-grayscale",
35-
eRemoveDotBg: "e-removedotbg",
36-
eUpscale: "e-upscale",
37-
eRetouch: "e-retouch",
38-
eGenvar: "e-genvar",
39-
eDropshadow: "e-dropshadow",
40-
eChangeBg: "e-changebg",
41-
eBgRemove: "e-bgremove",
42-
eContrast: "e-contrast",
43-
eShadow: "e-shadow",
44-
eSharpen: "e-sharpen",
45-
eUSM: "e-usm",
46-
eGradient: "e-gradient",
47-
effectContrast: "e-contrast",
48-
effectShadow: "e-shadow",
34+
35+
// Old deprecated mappings
4936
effectSharpen: "e-sharpen",
5037
effectUSM: "e-usm",
51-
effectGradient: "e-gradient",
38+
effectContrast: "e-contrast",
5239
effectGray: "e-grayscale",
40+
effectShadow: "e-shadow",
41+
effectGradient: "e-gradient",
42+
43+
// AI & advanced effects
44+
grayscale: "e-grayscale",
45+
aiBGRemoveExternal: "e-removedotbg",
46+
aiUpscale: "e-upscale",
47+
aiRetouch: "e-retouch",
48+
aiVariation: "e-genvar",
49+
aiDropShadow: "e-dropshadow",
50+
aiChangeBackground: "e-changebg",
51+
aiRemoveBackground: "e-bgremove",
52+
contrastStretch: "e-contrast",
53+
shadow: "e-shadow",
54+
sharpen: "e-sharpen",
55+
unsharpMask: "e-usm",
56+
gradient: "e-gradient",
57+
58+
// Other flags & finishing
5359
progressive: "pr",
5460
lossless: "lo",
5561
colorProfile: "cp",
@@ -58,8 +64,13 @@ const supportedTransforms: { [key: string]: string } = {
5864
trim: "t",
5965
zoom: "z",
6066
page: "pg",
67+
68+
69+
70+
// Raw pass-through
6171
raw: "raw",
62-
}
72+
};
73+
6374

6475

6576
export default supportedTransforms

src/interfaces/Transformation.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,45 +173,45 @@ export interface Transformation {
173173
/**
174174
* Enable grayscale effect for images.
175175
*/
176-
grayScale?: true;
176+
grayscale?: true;
177177

178178
/**
179179
* Use third-party background removal.
180180
* See also `removeBackground` for ImageKit's in-house background removal which is 90% cheaper.
181181
*/
182-
removeBackgroundThirdParty?: true
182+
aiBGRemoveExternal?: true
183183

184184
/**
185185
* Upscale images beyond their original dimensions with AI.
186186
*/
187-
upscale?: true
187+
aiUpscale?: true
188188

189189
/**
190190
* Retouch (AI-based) for improving faces or product shots.
191191
*/
192-
retouch?: true
192+
aiRetouch?: true
193193

194194
/**
195195
* Generate variation of an image using AI. This will generate a new image with slight variations from the original image. The variations include changes in color, texture, and other visual elements. However, the model will try to preserve the structure and essence of the original image.
196196
*/
197-
generateVariation?: true
197+
aiVariation?: true
198198

199199
/**
200200
* Add an AI-based drop shadow around a foreground object on a transparent or removed background.
201201
* You can control the direction, elevation, and saturation of the light source. E.g. change light direction `az-45`.
202202
* @link https://imagekit.io/docs/ai-transformations#ai-drop-shadow-e-dropshadow
203203
*/
204-
AIDropshadow?: string
204+
aiDropShadow?: string
205205

206206
/**
207207
* Change background using AI. Provide a prompt or base64-encoded prompt. e.g. `prompt-snow road` or `prompte-[urlencoded_base64_encoded_text]`.
208208
*/
209-
changeBackground?: string;
209+
aiChangeBackground?: string;
210210

211211
/**
212212
* ImageKit’s in-house background removal.
213213
*/
214-
removeBackground?: true
214+
aiRemoveBackground?: true
215215

216216
/**
217217
* Auto-enhance contrast for an image (contrast stretch).
@@ -243,7 +243,7 @@ export interface Transformation {
243243
/**
244244
* Used to specify whether the output JPEG image must be rendered progressively. In progressive loading, the output image renders as a low-quality pixelated full image, which, over time, keeps on adding more pixels and information to the image. This helps you maintain a fast perceived load time.
245245
*/
246-
progressiveJPEG?: boolean;
246+
progressive?: boolean;
247247

248248
/**
249249
* Used to specify whether the output image (if in JPEG or PNG) must be compressed losslessly.
@@ -291,5 +291,42 @@ export interface Transformation {
291291
* Pass any transformation that is not directly supported by the SDK. This transformation is passed as it is in the URL.
292292
*/
293293
raw?: string;
294+
295+
// old as it is but deprecated
296+
297+
/**
298+
* @deprecated Use `rotation` instead.
299+
*/
300+
rotate?: string;
301+
302+
/**
303+
* @deprecated Use `sharpen` instead.
304+
*/
305+
effectSharpen?: string;
306+
307+
/**
308+
* @deprecated Use `unsharpMask` instead.
309+
*/
310+
effectUSM?: string;
311+
312+
/**
313+
* @deprecated Use `contrastStretch` instead.
314+
*/
315+
effectContrast?: string;
316+
317+
/**
318+
* @deprecated Use `grayscale` instead.
319+
*/
320+
effectGray?: string;
321+
322+
/**
323+
* @deprecated Use `shadow` instead.
324+
*/
325+
effectShadow?: string;
326+
327+
/**
328+
* @deprecated Use `gradient` instead.
329+
*/
330+
effectGradient?: string;
294331
}
295332

src/url/builder.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,24 @@ function constructTransformationString(transformation: Transformation[] | undefi
8282
for (var i = 0, l = transformation.length; i < l; i++) {
8383
var parsedTransformStep = [];
8484
for (var key in transformation[i]) {
85-
if(transformation[i][key as keyof Transformation] === undefined || transformation[i][key as keyof Transformation] === null )
86-
continue;
85+
let value = transformation[i][key as keyof Transformation];
86+
if (value === undefined || value === null) {
87+
continue;
88+
}
89+
8790
var transformKey = transformationUtils.getTransformKey(key);
8891
if (!transformKey) {
8992
transformKey = key;
9093
}
9194

92-
if (transformation[i][key as keyof Transformation] === "-") {
95+
if (value === "-") value = true;
96+
97+
if (["e-grayscale", "e-contrast", "e-removedotbg", "e-bgremove", "e-upscale", "e-retouch", "e-genvar"].includes(transformKey) && value === true) {
9398
parsedTransformStep.push(transformKey);
9499
} else if (key === "raw") {
95100
parsedTransformStep.push(transformation[i][key]);
96101
} else {
97-
var value = transformation[i][key as keyof Transformation];
102+
value = transformation[i][key as keyof Transformation];
98103
if (transformKey === "di") {
99104
value = removeTrailingSlash(removeLeadingSlash(value as string || ""));
100105
value = value.replace(/\//g, "@@");

test/url-generation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ describe("URL generation", function () {
317317
}]
318318
})
319319

320-
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:h-300,w-400,ar-4-3,q-40,c-force,cm-extract,fo-left,f-jpeg,r-50,bg-A94D34,b-5-A94D34,rt-90,bl-10,n-some_name,pr-true,lo-true,t-5,md-true,cp-true,di-folder@@file.jpg,dpr-3,e-sharpen-10,e-usm-2-2-0.8-0.024,e-contrast-true,e-grayscale-true,e-shadow-bl-15_st-40_x-10_y-N5,e-gradient-from-red_to-white,orig-true,h-200,w-300,l-image,i-logo.png,l-end/test_path.jpg`);
320+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:h-300,w-400,ar-4-3,q-40,c-force,cm-extract,fo-left,f-jpeg,r-50,bg-A94D34,b-5-A94D34,rt-90,bl-10,n-some_name,pr-true,lo-true,t-5,md-true,cp-true,di-folder@@file.jpg,dpr-3,e-sharpen-10,e-usm-2-2-0.8-0.024,e-contrast,e-grayscale,e-shadow-bl-15_st-40_x-10_y-N5,e-gradient-from-red_to-white,orig-true,h-200,w-300,l-image,i-logo.png,l-end/test_path.jpg`);
321321
});
322322
});
323323

0 commit comments

Comments
 (0)