Skip to content

Commit 22af964

Browse files
authored
Merge pull request #84 from imagekit-developer/SDK-94
Added transformation, e-shadow, e-gradient parameter
2 parents 1c2eed5 + f0d610c commit 22af964

File tree

9 files changed

+346
-189
lines changed

9 files changed

+346
-189
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ Javascript SDK for [ImageKit](https://imagekit.io/) provides URL generation for
1414

1515
ImageKit is complete media storage, optimization, and transformation solution that comes with an [image and video CDN](https://imagekit.io/features/imagekit-infrastructure). It can be integrated with your existing infrastructure - storage like AWS S3, web servers, your CDN, and custom domain names, allowing you to deliver optimized images in minutes with minimal code changes.
1616

17-
## Changelog - SDK Version 2.0.0
18-
### Breaking changes
17+
## Changelog
18+
### SDK Version 3.0.0
19+
#### Breaking changes
20+
**1. Overlay syntax update**
21+
* In version 3.0.0, we've removed the old overlay syntax parameters for transformations, such as `oi`, `ot`, `obg`, and [more](https://docs.imagekit.io/features/image-transformations/overlay). These parameters are deprecated and will start returning errors when used in URLs. Please migrate to the new layers syntax that supports overlay nesting, provides better positional control, and allows more transformations at the layer level. You can start with [examples](https://docs.imagekit.io/features/image-transformations/overlay-using-layers#examples) to learn quickly.
22+
* You can migrate to the new layers syntax using the `raw` transformation parameter.
23+
### SDK Version 2.0.0
24+
#### Breaking changes
1925
**1. Authentication Process Update:**
2026
* Previously, when using client side file upload, the SDK required the `publicKey` and `authenticationEndpoint` parameters during SDK initialization to fetch security parameters (`signature`, `token`, and `expire`).
2127
* In version 2.0.0, we've updated the authentication process for the SDK. As a user of the SDK, you are now responsible for generating the security parameters (`signature`, `token`, and `expire`) yourself. This means you no longer need to provide the `authenticationEndpoint`. When using the SDK's upload method, make sure to pass these security parameters explicitly along with other [upload options](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload). For guidance on generating these security parameters, please refer to the documentation available [here](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload#signature-generation-for-client-side-file-upload).
@@ -260,6 +266,27 @@ var imageURL = imagekit.url({
260266
https://ik.imagekit.io/your_imagekit_id/tr:h-300,w-400,l-image,i-ik_canvas,bg-FF0000,w-300,h-100,l-end/img/sample-video.mp4
261267
```
262268

269+
**4. Arithmetic expressions in transformations**
270+
271+
ImageKit allows use of [arithmetic expressions](https://docs.imagekit.io/features/arithmetic-expressions-in-transformations) in certain dimension and position-related parameters, making media transformations more flexible and dynamic.
272+
273+
For example:
274+
275+
```js
276+
var imageURL = imagekit.url({
277+
src: "https://ik.imagekit.io/your_imagekit_id/default-image.jpg",
278+
transformation: [{
279+
"width": "iw_div_4",
280+
"height": "ih_div_2",
281+
"border": "cw_mul_0.05_yellow"
282+
}]
283+
});
284+
```
285+
286+
**Sample Result URL**
287+
```
288+
https://ik.imagekit.io/your_imagekit_id/default-image.jpg?tr=w-iw_div_4,h-ih_div_2,b-cw_mul_0.05_yellow
289+
```
263290

264291
#### List of supported transformations
265292

@@ -296,6 +323,8 @@ If you want to generate transformations in your application and add them to the
296323
| effectUSM | e-usm |
297324
| effectContrast | e-contrast |
298325
| effectGray | e-grayscale |
326+
| effectShadow | e-shadow |
327+
| effectGradient | e-gradient |
299328
| original | orig |
300329
| raw | The string provided in raw will be added in the URL as it is. |
301330

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imagekit-javascript",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"description": "Javascript SDK for using ImageKit.io in the browser",
55
"main": "dist/imagekit.cjs.js",
66
"module": "dist/imagekit.esm.js",

src/constants/errorMessages.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ export default {
1818
MISSING_SIGNATURE: { message: "Missing signature for upload. The SDK expects token, sginature and expire for authentication.", help: ""},
1919
MISSING_TOKEN: { message: "Missing token for upload. The SDK expects token, sginature and expire for authentication.", help: ""},
2020
MISSING_EXPIRE: { message: "Missing expire for upload. The SDK expects token, sginature and expire for authentication.", help: ""},
21+
INVALID_TRANSFORMATION: { message: "Invalid transformation parameter. Please include at least pre, post, or both.", help: ""},
22+
INVALID_PRE_TRANSFORMATION: { message: "Invalid pre transformation parameter.", help: ""},
23+
INVALID_POST_TRANSFORMATION: { message: "Invalid post transformation parameter.", help: ""},
2124
};

src/constants/supportedTransforms.ts

Lines changed: 10 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -87,141 +87,6 @@ const supportedTransforms: { [key: string]: string } = {
8787
*/
8888
named: "n",
8989

90-
/**
91-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-image-oi
92-
*/
93-
overlayImage: "oi",
94-
95-
/**
96-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-image-aspect-ratio-oiar
97-
*/
98-
overlayImageAspectRatio: "oiar",
99-
100-
/**
101-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-image-background-oibg
102-
*/
103-
overlayImageBackground: "oibg",
104-
105-
/**
106-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-image-border-oib
107-
*/
108-
overlayImageBorder: "oib",
109-
110-
/**
111-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-image-dpr-oidpr
112-
*/
113-
overlayImageDPR: "oidpr",
114-
115-
/**
116-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-image-quality-oiq
117-
*/
118-
overlayImageQuality: "oiq",
119-
120-
/**
121-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-image-cropping
122-
*/
123-
overlayImageCropping: "oic",
124-
125-
/**
126-
* @link https://docs.imagekit.io/features/image-transformations/overlay#trimming-of-the-overlay-image
127-
*/
128-
overlayImageTrim: "oit",
129-
130-
/**
131-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-x-position-ox
132-
*/
133-
overlayX: "ox",
134-
135-
/**
136-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-y-position-oy
137-
*/
138-
overlayY: "oy",
139-
140-
/**
141-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-focus-ofo
142-
*/
143-
overlayFocus: "ofo",
144-
145-
/**
146-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-height-oh
147-
*/
148-
overlayHeight: "oh",
149-
150-
/**
151-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-width-ow
152-
*/
153-
overlayWidth: "ow",
154-
155-
/**
156-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-ot
157-
*/
158-
overlayText: "ot",
159-
160-
/**
161-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-size-ots
162-
*/
163-
overlayTextFontSize: "ots",
164-
165-
/**
166-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-font-otf
167-
*/
168-
overlayTextFontFamily: "otf",
169-
170-
/**
171-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-color-otc
172-
*/
173-
overlayTextColor: "otc",
174-
175-
/**
176-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-transparency-oa
177-
*/
178-
overlayTextTransparency: "oa",
179-
180-
/**
181-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-transparency-oa
182-
*/
183-
overlayAlpha: "oa",
184-
185-
/**
186-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-typography-ott
187-
*/
188-
overlayTextTypography: "ott",
189-
190-
/**
191-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-background-obg
192-
*/
193-
overlayBackground: "obg",
194-
195-
/**
196-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-encoded-ote
197-
*/
198-
overlayTextEncoded: "ote",
199-
200-
/**
201-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-width-otw
202-
*/
203-
overlayTextWidth: "otw",
204-
205-
/**
206-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-background-otbg
207-
*/
208-
overlayTextBackground: "otbg",
209-
210-
/**
211-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-padding-otp
212-
*/
213-
overlayTextPadding: "otp",
214-
215-
/**
216-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-text-inner-alignment-otia
217-
*/
218-
overlayTextInnerAlignment: "otia",
219-
220-
/**
221-
* @link https://docs.imagekit.io/features/image-transformations/overlay#overlay-radius-or
222-
*/
223-
overlayRadius: "or",
224-
22590
/**
22691
* @link https://docs.imagekit.io/features/image-transformations/resize-crop-and-other-transformations#progressive-image-pr
22792
*/
@@ -282,6 +147,16 @@ const supportedTransforms: { [key: string]: string } = {
282147
*/
283148
original: "orig",
284149

150+
/**
151+
* @link https://docs.imagekit.io/features/image-transformations/image-enhancement-and-color-manipulation#shadow-e-shadow
152+
*/
153+
effectShadow: "e-shadow",
154+
155+
/**
156+
* @link https://docs.imagekit.io/features/image-transformations/image-enhancement-and-color-manipulation#gradient-e-gradient
157+
*/
158+
effectGradient: "e-gradient",
159+
285160
/**
286161
* @link https://docs.imagekit.io/features/image-transformations/conditional-transformations
287162
*/

src/interfaces/UploadOptions.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
interface TransformationObject {
2+
type: "transformation";
3+
value: string;
4+
}
5+
6+
interface GifToVideoOrThumbnailObject {
7+
type: "gif-to-video" | "thumbnail";
8+
value?: string;
9+
}
10+
11+
interface AbsObject {
12+
type: "abs";
13+
value: string;
14+
protocol: "hls" | "dash";
15+
}
16+
17+
type PostTransformation = TransformationObject | GifToVideoOrThumbnailObject | AbsObject;
18+
19+
interface Transformation{
20+
pre?: string
21+
post?: PostTransformation[]
22+
}
123
/**
224
* Options used when uploading a file
325
*
@@ -113,6 +135,8 @@ export interface UploadOptions {
113135
*/
114136
customMetadata?: string | Record<string, string | number | boolean | Array<string | number | boolean>>
115137

138+
transformation?: Transformation
139+
116140
/**
117141
* Optional XMLHttpRequest object that you can send for upload API request. You can listen to `progress` and other events on this object for any custom logic.
118142
*/

src/upload/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ export const upload = (
3939
return
4040
}
4141

42+
if (uploadOptions.transformation) {
43+
if (!(Object.keys(uploadOptions.transformation).includes("pre") || Object.keys(uploadOptions.transformation).includes("post"))) {
44+
respond(true, errorMessages.INVALID_TRANSFORMATION, callback);
45+
return;
46+
}
47+
if (Object.keys(uploadOptions.transformation).includes("pre") && !uploadOptions.transformation.pre) {
48+
respond(true, errorMessages.INVALID_PRE_TRANSFORMATION, callback);
49+
return;
50+
}
51+
if (Object.keys(uploadOptions.transformation).includes("post")) {
52+
if (Array.isArray(uploadOptions.transformation.post)) {
53+
for (let transformation of uploadOptions.transformation.post) {
54+
if (transformation.type === "abs" && !(transformation.protocol || transformation.value)) {
55+
respond(true, errorMessages.INVALID_POST_TRANSFORMATION, callback);
56+
return;
57+
} else if (transformation.type === "transformation" && !transformation.value) {
58+
respond(true, errorMessages.INVALID_POST_TRANSFORMATION, callback);
59+
return;
60+
}
61+
}
62+
} else {
63+
respond(true, errorMessages.INVALID_POST_TRANSFORMATION, callback);
64+
return;
65+
}
66+
}
67+
}
68+
4269
var formData = new FormData();
4370
let key: keyof typeof uploadOptions;
4471
for (key in uploadOptions) {
@@ -60,6 +87,9 @@ export const upload = (
6087
} else if (key === "customMetadata" && typeof uploadOptions.customMetadata === "object" &&
6188
!Array.isArray(uploadOptions.customMetadata) && uploadOptions.customMetadata !== null) {
6289
formData.append('customMetadata', JSON.stringify(uploadOptions.customMetadata));
90+
} else if(key === "transformation" && typeof uploadOptions.transformation === "object" &&
91+
uploadOptions.transformation !== null) {
92+
formData.append(key, JSON.stringify(uploadOptions.transformation));
6393
} else if(uploadOptions[key] !== undefined) {
6494
formData.append(key, String(uploadOptions[key]));
6595
}

src/url/builder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ 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] === undefined || transformation[i][key] === null )
86+
continue;
8587
var transformKey = transformationUtils.getTransformKey(key);
8688
if (!transformKey) {
8789
transformKey = key;
@@ -93,7 +95,7 @@ function constructTransformationString(transformation: Transformation[] | undefi
9395
parsedTransformStep.push(transformation[i][key]);
9496
} else {
9597
var value = transformation[i][key];
96-
if (transformKey === "oi" || transformKey === "di") {
98+
if (transformKey === "di") {
9799
value = removeTrailingSlash(removeLeadingSlash(value || ""));
98100
value = value.replace(/\//g, "@@");
99101
}

0 commit comments

Comments
 (0)