From e78489b36e5d06055c3c18091f362fc7d4a1077f Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 13 Apr 2014 18:03:59 +0700 Subject: [PATCH] Update unit test and changelog --- CHANGELOG.md | 1 + src/PhpWord/Element/Image.php | 11 ++++++----- tests/PhpWord/Tests/Element/ImageTest.php | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 937930e8..70f53abf 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ This release marked heavy refactorings on internal code structure with the creat - Endnote: Ability to add endnotes - @ivanlanin - ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198 - ODT Writer: Basic table writing support - @ivanlanin +- Image: Keep image aspect ratio if only 1 dimension styled - @japonicus GH-194 ### Bugfixes diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index 139c578f..ce4b1609 100755 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -133,14 +133,15 @@ class Image extends AbstractElement $this->style = $this->setStyle(new ImageStyle(), $style, true); $styleWidth = $this->style->getWidth(); $styleHeight = $this->style->getHeight(); + list($actualWidth, $actualHeight) = $imgData; 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])); + $this->style->setWidth($actualWidth); + $this->style->setHeight($actualHeight); + } elseif ($styleWidth) { + $this->style->setHeight($actualHeight * ($styleWidth / $actualWidth)); } else { - $this->style->setWidth($imgData[0] * ($styleHeight / $imgData[1])); + $this->style->setWidth($actualWidth * ($styleHeight / $actualHeight)); } } $this->setImageFunctions(); diff --git a/tests/PhpWord/Tests/Element/ImageTest.php b/tests/PhpWord/Tests/Element/ImageTest.php index 6ec6743a..eae9fc5f 100644 --- a/tests/PhpWord/Tests/Element/ImageTest.php +++ b/tests/PhpWord/Tests/Element/ImageTest.php @@ -129,7 +129,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase public function testPNG() { $src = __DIR__ . "/../_files/images/firefox.png"; - $oImage = new Image($src); + $oImage = new Image($src, array('width' => 100)); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getSource(), $src); @@ -146,7 +146,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase public function testGIF() { $src = __DIR__ . "/../_files/images/mario.gif"; - $oImage = new Image($src); + $oImage = new Image($src, array('height' => 100)); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getSource(), $src);