Skip to content

Commit

Permalink
feat(preserveAspectRatio): added option to preserve aspect ratio
Browse files Browse the repository at this point in the history
When set to true and both width and height are specified, they are interpreted as the minimum width and minimum height, respectively.
If set to true with only the width specified, the height will be automatically determined while preserving the aspect ratio, and vice versa.

fixes #88, #189
  • Loading branch information
mskec committed Nov 18, 2023
1 parent 979b802 commit 4f84d93
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/graphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export class Graphics {

private height = 512;

private preserveAspectRatio = false;

private density = 72;

private savePath = "./";
Expand All @@ -37,6 +39,7 @@ export class Graphics {
}

public gmBaseCommand(stream: fs.ReadStream, filename: string): gm.State {
// console.log(JSON.stringify({ width: this.width, height: this.height, preserveAspectRatio: this.preserveAspectRatio, density: this.density }))
return this.gm(stream, filename)
.density(this.density, this.density)
.resize(this.width, this.height, this.preserveAspectRatio ? '^' : '!')
Expand Down Expand Up @@ -136,7 +139,13 @@ export class Graphics {

public setSize(width: number, height?: number): Graphics {
this.width = width;
this.height = !!height ? height : width;
this.height = this.preserveAspectRatio || !!height ? height : width;

return this;
}

public setPreserveAspectRatio(preserveAspectRatio: boolean): Graphics {
this.preserveAspectRatio = preserveAspectRatio;

return this;
}
Expand Down Expand Up @@ -189,6 +198,7 @@ export class Graphics {
format: this.format,
width: this.width,
height: this.height,
preserveAspectRatio: this.preserveAspectRatio,
density: this.density,
savePath: this.savePath,
saveFilename: this.saveFilename,
Expand Down

0 comments on commit 4f84d93

Please sign in to comment.