Skip to content

Commit

Permalink
feat: add sharp trim feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pmb0 authored Jun 14, 2022
2 parents 7d3bc6a + 6eac25f commit c511a19
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ Currently the following transformations can be applied to images:
| format | `f` | Output image format. Valid values: every valid [sharp output format string](https://sharp.pixelplumbing.com/api-output#toformat), i.e. `jpeg`, `gif`, `webp` or `raw`. |
| progressive | `p` | Only available for jpeg and png formats. Enable progressive scan by passing `true`. |
| crop | `c` | Setting crop to `true` enables the [sharp cropping feature](https://sharp.pixelplumbing.com/api-resize#crop). Note: Both `width` and `height` params are neccessary for crop to work. Default is `false`. |
| trim | `t` | Setting trim to `true` enables the [sharp trim feature](https://sharp.pixelplumbing.com/api-resize#trim). |
| gravity | `g` | When the crop option is activated you can specify the gravity of the cropping. Possible attributes of the optional `gravity` are `north`, `northeast`, `east`, `southeast`, `south`, `southwest`, `west`, `northwest`, `center` and `centre`. Default is `center`. |
# License
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export enum QueryParams {
format = 'f',
progressive = 'p',
crop = 'c',
trim = 't',
gravity = 'g',
}

Expand Down
4 changes: 4 additions & 0 deletions src/resize.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class ResizeDto {
@IsBoolean()
public crop: boolean = false

@Transform((value) => value === 'true')
@IsBoolean()
public trim: boolean = false

@IsIn([
'north',
'northeast',
Expand Down
4 changes: 4 additions & 0 deletions src/transformer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class Transformer {
options.format = (await transformer.metadata()).format as format
}

if (options.trim) {
transformer.trim()
}

if (options.crop) {
const [cropWidth, cropHeight] = this.getCropDimensions(
this.cropMaxSize,
Expand Down

0 comments on commit c511a19

Please sign in to comment.