Skip to content

Commit 833dfea

Browse files
author
japonicus
committed
Keep image aspect ratio if only 1 dimension styled
If only one of image width or height is specified, then scale missing dimension to maintain the aspect ratio.
1 parent cf7263b commit 833dfea

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/PhpWord/Element/Image.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,17 @@ public function __construct($source, $style = null, $isWatermark = false)
131131
$this->source = $source;
132132
$this->isWatermark = $isWatermark;
133133
$this->style = $this->setStyle(new ImageStyle(), $style, true);
134-
if ($this->style->getWidth() == null && $this->style->getHeight() == null) {
135-
$this->style->setWidth($imgData[0]);
136-
$this->style->setHeight($imgData[1]);
134+
$styleWidth = $this->style->getWidth();
135+
$styleHeight = $this->style->getHeight();
136+
if (!($styleWidth && $styleHeight)) {
137+
if ($styleWidth == null && $styleHeight == null) {
138+
$this->style->setWidth($imgData[0]);
139+
$this->style->setHeight($imgData[1]);
140+
} else if ($styleWidth) {
141+
$this->style->setHeight($imgData[1] * ($styleWidth / $imgData[0]));
142+
} else {
143+
$this->style->setWidth($imgData[0] * ($styleHeight / $imgData[1]));
144+
}
137145
}
138146
$this->setImageFunctions();
139147
}

0 commit comments

Comments
 (0)