Skip to content

Commit

Permalink
Add autoPad resize action (#46)
Browse files Browse the repository at this point in the history
* Add autoPad resize action

* Remove unneccessary model

* Rename test case

* Lint fixes
  • Loading branch information
magdakwiecien authored Feb 21, 2024
1 parent 1cd3594 commit bd8dc9a
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 5 deletions.
19 changes: 19 additions & 0 deletions __TESTS__/unit/actions/Resize/AutoPadResizeAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Transformation} from "../../../../src";
import {Resize, autoPad} from "../../../../src/actions/resize";
import {Background} from "../../../../src/qualifiers";

describe('Tests for Transformation Action -- Resize.autoPad', () => {
it('Ensures it generates the right transformation', () => {
const tx = new Transformation().resize(autoPad(250, 250)).toString();
expect(tx).toContain('c_auto_pad,g_auto,h_250,w_250');
});

it('Ensures it generates the right transformation using qualifiers', () => {
const tx = new Transformation().resize(
Resize.autoPad()
.width(250)
.height(250)
.background(Background.color('red'))).toString();
expect(tx).toContain('b_red,c_auto_pad,g_auto,h_250,w_250');
});
});
7 changes: 6 additions & 1 deletion __TESTS__/unit/fromJson/resize.fromJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ describe('resize.fromJson', () => {
}
},
{actionType: 'auto', dimensions: {width: 100, height: 200}, gravity: {gravityType: 'direction', compass: 'south'}},
{actionType: 'autoPad', dimensions: {width: 100, height: 200}, background: {
backgroundType: 'color',
color: 'red'
}}
]});

expect(transformation.toString()).toStrictEqual([
Expand All @@ -78,7 +82,8 @@ describe('resize.fromJson', () => {
'c_crop,g_dog:auto:bird_30:cat_avoid,w_200',
'b_gen_fill:prompt_hello,c_pad,w_200',
'ar_7.0,b_gen_fill,c_mpad,w_200',
'c_auto,g_south,h_200,w_100'
'c_auto,g_south,h_200,w_100',
'b_red,c_auto_pad,g_auto,h_200,w_100'
].join('/'));
});

Expand Down
19 changes: 19 additions & 0 deletions __TESTS__/unit/toJson/resize.toJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,23 @@ describe('resize.toJson()', () => {
});
});

it('autoPad', () => {
const transformation = new Transformation()
.addAction(Resize.autoPad().width(200).height(100).background('red'));
expect(transformation.toJson()).toStrictEqual({
actions: [
{
"actionType": "autoPad",
"dimensions": {
"width": 200,
"height": 100,
},
background: {
backgroundType: "color",
color: "red"
},
},
]
});
});
});
23 changes: 21 additions & 2 deletions src/actions/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {ResizeLimitFillAction} from "./resize/ResizeLimitFillAction.js";
import {ResizeLimitPadAction} from "./resize/ResizeLimitPadAction.js";
import {ResizeMinimumPadAction} from "./resize/ResizeMinimumPadAction.js";
import {ResizeAdvancedAction} from "./resize/ResizeAdvancedAction.js";
import {ResizeAutoPadAction} from "./resize/ResizeAutoPadAction.js";

/**
* @summary action
Expand Down Expand Up @@ -309,6 +310,22 @@ function limitPad(width?: string|number, height?: string|number) :ResizeLimitPad
}


/**
* @summary action
* @description
* Tries to prevent a "bad crop" by first attempting to use the auto cropping mode, but adding some padding
* if the algorithm determines that more of the original image needs to be included in the final image.
*
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeAutoPadAction}
*/
function autoPad(width?: string|number, height?: string|number): ResizeAutoPadAction {
return new ResizeAutoPadAction('auto_pad', width, height);
}


const Resize = {
imaggaScale,
imaggaCrop,
Expand All @@ -324,7 +341,8 @@ const Resize = {
minimumFit,
limitPad,
fillPad,
auto
auto,
autoPad
};
export {
Resize,
Expand All @@ -342,5 +360,6 @@ export {
minimumFit,
limitPad,
fillPad,
auto
auto,
autoPad
};
40 changes: 40 additions & 0 deletions src/actions/resize/ResizeAutoPadAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {BackgroundQualifier} from "../../qualifiers/background/shared/base/BackgroundQualifier.js";
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
import {IActionModel} from "../../internal/models/IActionModel.js";
import {createBackgroundModel, IBackgroundModel} from "../../internal/models/createBackgroundModel.js";
import {createBackgroundFromModel} from "../../internal/models/createBackgroundFromModel.js";
import {ResizeSimpleAction} from "./ResizeSimpleAction.js";

/**
* @description Tries to prevent a "bad crop" by first attempting to use the auto cropping mode, but adding some padding if the algorithm determines that more of the original image needs to be included in the final image.
* @extends Actions.Resize.autoPad
* @memberOf Actions.Resize
* @see Visit {@link Actions.Resize| Resize} for examples
*/
class ResizeAutoPadAction extends ResizeSimpleAction {
constructor(cropType: string, cropWidth: number | string, cropHeight?: number | string) {
super(cropType, cropWidth, cropHeight);

this.addQualifier(new Qualifier('g', 'auto'));
}

/**
* @description Sets the background.
* @param {Qualifiers.Background} backgroundQualifier Defines the background color to use instead of
* transparent background areas or when resizing with padding.
*/
background(backgroundQualifier: BackgroundQualifier | string): this {
this._actionModel.background = createBackgroundModel(backgroundQualifier);
return this.addQualifier(backgroundQualifier);
}

static fromJson(actionModel: IActionModel): ResizeAutoPadAction {
const result = super.fromJson.apply(this, [actionModel]);
actionModel.background && result.background(createBackgroundFromModel(actionModel.background as IBackgroundModel));

return result;
}
}


export {ResizeAutoPadAction};
4 changes: 3 additions & 1 deletion src/internal/fromJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import { GenerativeReplace } from "../actions/effect/GenerativeReplace.js";
import { GenerativeRecolor } from "../actions/effect/GenerativeRecolor.js";
import {ResizeAdvancedAction} from "../actions/resize/ResizeAdvancedAction.js";
import {BackgroundColor} from "../actions/background/actions/BackgroundColor.js";
import {ResizeAutoPadAction} from "../actions/resize/ResizeAutoPadAction.js";

const ActionModelMap: Record<string, IHasFromJson> = {
scale: ResizeScaleAction,
Expand Down Expand Up @@ -152,7 +153,8 @@ const ActionModelMap: Record<string, IHasFromJson> = {
upscale: SimpleEffectAction,
auto: ResizeAdvancedAction,
backgroundColor: BackgroundColor,
enhance: SimpleEffectAction,
autoPad: ResizeAutoPadAction,
enhance: SimpleEffectAction
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/internal/internalConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export const ACTION_TYPE_TO_CROP_MODE_MAP: Record<string, string> = {
minimumFit: 'mfit',
thumbnail: 'thumb',
limitPad: 'lpad',
minimumPad: 'mpad'
minimumPad: 'mpad',
autoPad: 'auto_pad'
};

export const ACTION_TYPE_TO_DELIVERY_MODE_MAP: Record<string, string> = {
Expand Down

0 comments on commit bd8dc9a

Please sign in to comment.