Skip to content

Commit 76fedd8

Browse files
committed
Add unit tests for image transformations and overlays
- Implement tests for buildTransformationString to validate transformation string generation. - Create comprehensive tests for overlay transformations, including text, image, video, subtitle, and solid color overlays. - Ensure proper handling of invalid overlay values and encoding scenarios. - Validate URL generation for various overlay types and nested transformations.
1 parent 6a0ff10 commit 76fedd8

File tree

5 files changed

+2511
-6
lines changed

5 files changed

+2511
-6
lines changed

src/lib/transformation-utils.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Transformation utilities ported from JavaScript SDK
2+
// This file is in src/lib/ to avoid conflicts with generated code
3+
4+
import type { SrcOptions, TransformationPosition } from '../resources/shared';
5+
6+
const QUERY_TRANSFORMATION_POSITION: TransformationPosition = 'query';
7+
const PATH_TRANSFORMATION_POSITION: TransformationPosition = 'path';
8+
const CHAIN_TRANSFORM_DELIMITER: string = ':';
9+
const TRANSFORM_DELIMITER: string = ',';
10+
const TRANSFORM_KEY_VALUE_DELIMITER: string = '-';
11+
12+
/**
13+
* Supported transformations mapping
14+
* {@link https://imagekit.io/docs/transformations}
15+
*/
16+
export const supportedTransforms: { [key: string]: string } = {
17+
// Basic sizing & layout
18+
width: 'w',
19+
height: 'h',
20+
aspectRatio: 'ar',
21+
background: 'bg',
22+
border: 'b',
23+
crop: 'c',
24+
cropMode: 'cm',
25+
dpr: 'dpr',
26+
focus: 'fo',
27+
quality: 'q',
28+
x: 'x',
29+
xCenter: 'xc',
30+
y: 'y',
31+
yCenter: 'yc',
32+
format: 'f',
33+
videoCodec: 'vc',
34+
audioCodec: 'ac',
35+
radius: 'r',
36+
rotation: 'rt',
37+
blur: 'bl',
38+
named: 'n',
39+
defaultImage: 'di',
40+
flip: 'fl',
41+
original: 'orig',
42+
startOffset: 'so',
43+
endOffset: 'eo',
44+
duration: 'du',
45+
streamingResolutions: 'sr',
46+
47+
// AI & advanced effects
48+
grayscale: 'e-grayscale',
49+
aiUpscale: 'e-upscale',
50+
aiRetouch: 'e-retouch',
51+
aiVariation: 'e-genvar',
52+
aiDropShadow: 'e-dropshadow',
53+
aiChangeBackground: 'e-changebg',
54+
aiRemoveBackground: 'e-bgremove',
55+
aiRemoveBackgroundExternal: 'e-removedotbg',
56+
contrastStretch: 'e-contrast',
57+
shadow: 'e-shadow',
58+
sharpen: 'e-sharpen',
59+
unsharpMask: 'e-usm',
60+
gradient: 'e-gradient',
61+
62+
// Other flags & finishing
63+
progressive: 'pr',
64+
lossless: 'lo',
65+
colorProfile: 'cp',
66+
metadata: 'md',
67+
opacity: 'o',
68+
trim: 't',
69+
zoom: 'z',
70+
page: 'pg',
71+
72+
// Text overlay transformations
73+
fontSize: 'fs',
74+
fontFamily: 'ff',
75+
fontColor: 'co',
76+
innerAlignment: 'ia',
77+
padding: 'pa',
78+
alpha: 'al',
79+
typography: 'tg',
80+
lineHeight: 'lh',
81+
82+
// Subtitles transformations
83+
fontOutline: 'fol',
84+
fontShadow: 'fsh',
85+
86+
// Raw pass-through
87+
raw: 'raw',
88+
89+
// Additional missing mappings from JS SDK
90+
aiEdit: 'e-edit',
91+
};
92+
93+
export default {
94+
addAsQueryParameter: (options: SrcOptions): boolean => {
95+
return options.transformationPosition === QUERY_TRANSFORMATION_POSITION;
96+
},
97+
getTransformKey: function (transform: string): string {
98+
if (!transform) {
99+
return '';
100+
}
101+
102+
return supportedTransforms[transform] || supportedTransforms[transform.toLowerCase()] || '';
103+
},
104+
getChainTransformDelimiter: function (): string {
105+
return CHAIN_TRANSFORM_DELIMITER;
106+
},
107+
getTransformDelimiter: function (): string {
108+
return TRANSFORM_DELIMITER;
109+
},
110+
getTransformKeyValueDelimiter: function (): string {
111+
return TRANSFORM_KEY_VALUE_DELIMITER;
112+
},
113+
};
114+
115+
export const safeBtoa = function (str: string): string {
116+
// Check if running in browser environment
117+
if (typeof globalThis !== 'undefined' && 'btoa' in globalThis) {
118+
return (globalThis as any).btoa(str);
119+
} else {
120+
// Node.js fallback
121+
return Buffer.from(str, 'utf8').toString('base64');
122+
}
123+
};

0 commit comments

Comments
 (0)