Skip to content

Commit

Permalink
Offset coordinates for cropping: x_5,y_5,w_80,h_60,c_crop (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya authored Sep 25, 2020
1 parent 08fdb5e commit c3860ab
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 8 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Image Resizing
# Cloud Image Resizing · [![npm package][npm-badge]][npm]

[npm-badge]: https://img.shields.io/npm/v/image-resizing.svg?style=flat-square
[npm]: https://www.npmjs.org/package/image-resizing

Node.js backend (middleware) for image manipulation needs (transform, resize,
optimize) that can be hosted in a serverless environment such as Google Cloud
Expand All @@ -8,6 +11,10 @@ Functions.

Create a Google Cloud Function project that exports image transformation HTTP handler:

```
$ npm install image-resizing --save
```

```js
const { createHandler } = require("image-resizing");

Expand All @@ -24,7 +31,9 @@ module.exports.img = createHandler({
Deploy it to GCP using Node.js v12+ runtime and configure a CDN on top of it.

```
https://example.com/w_80,h_60,c_fill/example.jpg
https://example.com/image.jpg - original image
https://example.com/w_80,h_60,c_fill/image.jpg - resized image (80x60)
https://example.com/x_10,y_10,w_80,h_60,c_crop/image.jpg - cropped image (80x60 at 10,10 offset)
```

## References
Expand All @@ -36,7 +45,7 @@ https://example.com/w_80,h_60,c_fill/example.jpg
## Contributing

Contributions of any kind are welcome! If you're unsure about something or need
directions, don't hesitate to get in touch on[Discord](https://discord.com/invite/bSsv7XM).
directions, don't hesitate to get in touch on [Discord](https://discord.com/invite/bSsv7XM).

## License

Expand Down
18 changes: 18 additions & 0 deletions lib/params.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/params.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/transform.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/transform.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export declare type Transform = {
width?: number;
height?: number;
crop?: "crop" | "fill";
x?: number;
y?: number;
};
export declare type Param<T> = {
readonly key: string;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "image-resizing",
"version": "0.1.0",
"version": "0.1.1",
"description": "Node.js backend (middleware) for image manipulation needs (transform, resize, optimize).",
"keywords": [
"cloud functions",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ export const params: Params = {
parse: (value) => value,
validate: (value) => ["crop", "fill"].includes(value),
} as Param<"crop" | "fill">,

x: {
key: "x",
parse: (value) => parseInt(value, 10),
validate: (value) => !isNaN(value),
} as Param<number>,

y: {
key: "y",
parse: (value) => parseInt(value, 10),
validate: (value) => !isNaN(value),
} as Param<number>,
};
22 changes: 22 additions & 0 deletions src/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,25 @@ it("/img/w_80,h_60,c_fill/w_60,h_40/example.jpg", () => {
}
`);
});

it("/img/x_10,y_5,w_80,h_60,c_crop/example.jpg", () => {
const result = parseUrlPath(
"/img/x_10,y_5,w_80,h_60,c_crop/example.jpg",
params,
);
expect(result).toMatchInlineSnapshot(`
Object {
"source": "img/example.jpg",
"target": "img/example__x_10,y_5,w_80,h_60,c_crop.jpg",
"transforms": Array [
Object {
"crop": "crop",
"height": 60,
"width": 80,
"x": 10,
"y": 5,
},
],
}
`);
});
17 changes: 17 additions & 0 deletions src/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,20 @@ it("h_60.png", (done) => {
},
);
});

it("x_260,y_160,w_120,h_80,c_crop.png", (done) => {
const transforms: Transform[] = [
{ x: 260, y: 160, width: 120, height: 80, crop: "crop" },
];
transform(fs.createReadStream("test.jpg"), transforms).toBuffer(
"png",
(err, image) => {
if (err) {
done(err);
} else {
expect(image).toMatchImageSnapshot();
done();
}
},
);
});
8 changes: 7 additions & 1 deletion src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export function transform(

transforms.forEach((params) => {
if (params.width && params.height) {
if (params.crop === "fill") {
if (
params.y !== undefined &&
params.y !== undefined &&
params.crop === "crop"
) {
state = state.crop(params.width, params.height, params.x, params.y);
} else if (params.crop === "fill") {
state = state
.resize(params.width, params.height, "^")
.gravity("Center")
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export type Transform = {
width?: number;
height?: number;
crop?: "crop" | "fill";
x?: number;
y?: number;
};

export type Param<T> = {
Expand Down

0 comments on commit c3860ab

Please sign in to comment.