Skip to content

Commit 4f84d93

Browse files
committed
feat(preserveAspectRatio): added option to preserve aspect ratio
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
1 parent 979b802 commit 4f84d93

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/graphics.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class Graphics {
1313

1414
private height = 512;
1515

16+
private preserveAspectRatio = false;
17+
1618
private density = 72;
1719

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

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

137140
public setSize(width: number, height?: number): Graphics {
138141
this.width = width;
139-
this.height = !!height ? height : width;
142+
this.height = this.preserveAspectRatio || !!height ? height : width;
143+
144+
return this;
145+
}
146+
147+
public setPreserveAspectRatio(preserveAspectRatio: boolean): Graphics {
148+
this.preserveAspectRatio = preserveAspectRatio;
140149

141150
return this;
142151
}
@@ -189,6 +198,7 @@ export class Graphics {
189198
format: this.format,
190199
width: this.width,
191200
height: this.height,
201+
preserveAspectRatio: this.preserveAspectRatio,
192202
density: this.density,
193203
savePath: this.savePath,
194204
saveFilename: this.saveFilename,

0 commit comments

Comments
 (0)