From 833dfea1e00adb8978d5a585fbb2052ade5b029e Mon Sep 17 00:00:00 2001 From: japonicus Date: Tue, 8 Apr 2014 17:42:01 +0100 Subject: [PATCH] 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. --- src/PhpWord/Element/Image.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index d49972d215..611b36b389 100755 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -131,9 +131,17 @@ public function __construct($source, $style = null, $isWatermark = false) $this->source = $source; $this->isWatermark = $isWatermark; $this->style = $this->setStyle(new ImageStyle(), $style, true); - if ($this->style->getWidth() == null && $this->style->getHeight() == null) { - $this->style->setWidth($imgData[0]); - $this->style->setHeight($imgData[1]); + $styleWidth = $this->style->getWidth(); + $styleHeight = $this->style->getHeight(); + if (!($styleWidth && $styleHeight)) { + if ($styleWidth == null && $styleHeight == null) { + $this->style->setWidth($imgData[0]); + $this->style->setHeight($imgData[1]); + } else if ($styleWidth) { + $this->style->setHeight($imgData[1] * ($styleWidth / $imgData[0])); + } else { + $this->style->setWidth($imgData[0] * ($styleHeight / $imgData[1])); + } } $this->setImageFunctions(); }